Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Allocation / Delete problems

Reply
Thread Tools

Allocation / Delete problems

 
 
Hamish Dean
Guest
Posts: n/a
 
      04-12-2004
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?


 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-13-2004
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.
 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      04-13-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
How should be delete for this allocation pavan734@gmail.com C++ 9 06-29-2006 11:36 PM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
memory allocation problems Kieran Simpson C++ 3 06-30-2003 05:14 AM



Advertisments