Allocation / Delete problems

Discussion in 'C++' started by Hamish Dean, Apr 13, 2004.

  1. Hamish Dean

    Hamish Dean Guest

    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?
     
    Hamish Dean, Apr 13, 2004
    #1
    1. Advertising

  2. Hamish Dean wrote:

    > 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?
    >


    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.
     
    Kevin Goodsell, Apr 13, 2004
    #2
    1. Advertising

  3. Hamish Dean

    Buster Guest

    Hamish Dean wrote:

    > 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;
    > */


    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).

    > Are there any problems with this code?


    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.
     
    Buster, Apr 13, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kieran Simpson

    memory allocation problems

    Kieran Simpson, Jun 30, 2003, in forum: C++
    Replies:
    3
    Views:
    372
    John Harrison
    Jun 30, 2003
  2. Ken
    Replies:
    24
    Views:
    3,903
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    1,007
    chris
    Oct 28, 2005
  4. Replies:
    9
    Views:
    304
    Ron Natalie
    Jun 30, 2006
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,208
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page