deallocating memory twice

B

binaya

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

tom_usenet

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
 
R

Ron Natalie

binaya said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top