C++ Deleting a pointer

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

C++ Deleting a pointer

Postby stuarte9 » Fri Aug 15, 2014 3:44 pm

Hi all,

Could some kind soul please help me to understand exactly what happens when a C++ pointer is deleted using "delete".

I fuzzily understand that the connection between the pointer and the memory that it points to is somehow broken.

Given that the pointer, after "delete" has executed and before it is set to NULL, still holds the address of that location in memory, in what way is the connection between the pointer and that location in memory broken ?

Thanks in advance for any help that you can give with this.

Best regards,

Stuart
stuarte9
 
Posts: 58
Joined: Mon Mar 08, 2010 5:03 pm
Location: Scotland

Re: C++ Deleting a pointer

Postby shaddack » Wed Oct 08, 2014 5:20 pm

Hi

The memory to which the pointer points to is returned to the operating system when delete is called, and thus is free to be allocated again. Or rather the memory block is returned to the pool of free memory blocks from which allocations with new is allocated from.

It may happen that the pointer variable still holds the address of the memory block that is now deleted, but it should be 0. Either implicitly or set to 0 explicitly so it is easy to check whether the pointer is valid or not.
Currently using openSuse 13.1
User avatar
shaddack
 
Posts: 75
Joined: Mon Mar 17, 2008 6:03 pm
Location: Gothenburg, Sweden

Re: C++ Deleting a pointer

Postby stuarte9 » Thu Oct 09, 2014 10:19 am

Good morning again shaddack,

Thanks for this reply also. You have successfully plugged a gap in my understanding.

Best regards,

Stuart
stuarte9
 
Posts: 58
Joined: Mon Mar 08, 2010 5:03 pm
Location: Scotland

Re: C++ Deleting a pointer

Postby shaddack » Thu Oct 09, 2014 6:28 pm

I'm happy to be able to help! :)
Currently using openSuse 13.1
User avatar
shaddack
 
Posts: 75
Joined: Mon Mar 17, 2008 6:03 pm
Location: Gothenburg, Sweden


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests