By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

deallocating memory twice

P: n/a
Hi all,

what actually goes wrong when I deallocated the memory twice ..?
say I have

int *p;
p=new(int);

now i run the command;

delete p;
delete p;

what actually happens behind the scene ?
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 6 Nov 2003 01:07:16 -0800, mo******@hotmail.com (binaya) wrote:
Hi all,

what actually goes wrong when I deallocated the memory twice ..?
say I have

int *p;
p=new(int);

now i run the command;

delete p;
Deletes the memory that p points to. p now points to this deallocated
memory region (and strictly speaking has an indeterminate value).
delete p;
This runs the deallocation algorithm on the same bit of memory, which
could do pretty much anything depending on the memory allocation
algorithm used. It might overwrite another bit of memory or similar.
Generally the effect of this is known as "heap corruption", and can
lead to crashes in code that runs much later.
what actually happens behind the scene ?


It depends on the memory allocator used by your compiler/library.

Tom
Jul 19 '05 #2

P: n/a

"binaya" <mo******@hotmail.com> wrote in message news:1c**************************@posting.google.c om...

delete p;
delete p;

what actually happens behind the scene ?


Undefined behavior. Depends on your environment. Perhaps it detects
you're freeing memory twice at runtime and issues an exception, perhaps
it just hoses up the allocation structures. Maybe it is a disaster of biblical proportions.
Fire and brimstone coming down from the skies. Rivers and seas boiling!
40 years of darkness, earthquakes, volcanos. The dead rising from the grave!
Human sacrifice, dogs and cats, living together... mass hysteria!
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.