Allocation / Delete problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hamish Dean

    Allocation / Delete problems

    Hi,

    I have created a DLL which the user is complaining:

    "Even when it works ok, my application stays instable (like something is
    allocated or deleted in the wrong way)."

    What does this mean? The way I have been allocating is:

    int * pNew;
    pNew = new int;

    And deletion:

    delete pNew;
    pNew = NULL; /* what woudl happen here if i left this line out? or pNew = 0;
    */

    Are there any problems with this code?


  • Kevin Goodsell

    #2
    Re: Allocation / Delete problems

    Hamish Dean wrote:
    [color=blue]
    > Hi,
    >
    > I have created a DLL which the user is complaining:
    >
    > "Even when it works ok, my application stays instable (like something is
    > allocated or deleted in the wrong way)."
    >
    > What does this mean? The way I have been allocating is:
    >
    > int * pNew;
    > pNew = new int;
    >
    > And deletion:
    >
    > delete pNew;
    > pNew = NULL; /* what woudl happen here if i left this line out? or pNew = 0;
    > */
    >
    > Are there any problems with this code?
    >[/color]

    Only of you expect 'pNew = NULL;' to set to null ALL the pointer that
    pointed to the deleted memory. If that's not the case, the problem is
    probably elsewhere. Explicit memory management opens the door for all
    kinds of errors, which is why it is best avoided.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • Buster

      #3
      Re: Allocation / Delete problems

      Hamish Dean wrote:
      [color=blue]
      > I have created a DLL which the user is complaining:
      >
      > "Even when it works ok, my application stays instable (like something is
      > allocated or deleted in the wrong way)."
      >
      > What does this mean? The way I have been allocating is:
      >
      > int * pNew;
      > pNew = new int;
      >
      > And deletion:
      >
      > delete pNew;
      > pNew = NULL; /* what woudl happen here if i left this line out? or pNew = 0;
      > */[/color]

      Nothing would happen, and "0" and "NULL" are, roughly speaking,
      the same. I prefer "0" because it's supported directly by the
      language and doesn't depend on a macro definition in whatever
      header it is.

      You set a pointer to 0 in order to indicate that it doesn't point
      to an object. This is sometimes useful. Sometimes the actual check
      can be elided if all you want to do is "delete the object pointed to,
      if any" (since "delete 0;" is a no-op). Sometimes you'll do an explicit
      comparison.

      If the pointer goes out of scope immediately after deletion,
      setting it to 0 is a waste of time (but not much time).
      [color=blue]
      > Are there any problems with this code?[/color]

      Not as written. But then, as written, it doesn't do anything.
      Perhaps the control flow is not exactly as you think, and the
      "delete" statement is not executed, or is executed more than
      once, in some circumstances. You should probably be using RAII.

      --
      Regards,
      Buster.

      Comment

      Working...