473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

deallocating memory twice

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
2 3531
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tino | last post by:
In TC++PL (third edition), Section 16.3.8, Stroustrup gives the following as a way to give memory from a std::vector back to the system: vector<int> tmp = v; // v.size() == 0 v.swap( tmp ); //...
5
by: AMT2K5 | last post by:
If I have the class class IOLabel : public IOField { private: int len; char format; public: IOLabel(int row, int col, int len):IOField(row, col)
5
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were...
2
by: Sheldon | last post by:
Hi, Can someone tell me how to remove the conflicts here ? #include <stdlib> static char*** Memory2DStr(int R, int C); static void MemoryFreeStr(int C, char*** array); void main() {...
4
by: divyabarathii | last post by:
hi i am working in VC++. In the foolowing code { int *p = NULL p = new int; delete p; }
8
by: Chris Portka | last post by:
I need to be able to allocate large numbers of elements at a time but then delete them one at a time. I have settled on the design of using new to allocate many items at once, but am unsure what...
4
by: Xegros | last post by:
hi there, i'm trying to get this get to work but after some clicks (the procedure activates after a click) some text gets messed up, it is some memory mess i guess. anyway take a look, tell me if...
6
by: Amit_Basnak | last post by:
Dear Friends I have two structures as below typedef struct { long_int length; char data; } CI_STRUCT_DATA; typedef CI_STRUCT_DATA *ptr_CiStructData;
27
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.