deleting pointers

Hi, if I have code like this:

void main()
{
int *pPointer = new int;
*pPointer = 25;
printf("Value of *pPointer: %d\n", *pPointer);

delete pPointer;

}

this would be a proper way to delete pointer objects right?
when I debug this code, pPointer doesn't get "deleted", it just points to this really "random" int:

-17891602 int

what's going on ?

thanks!
Last edited on
That is because the pointer doesn't get deleted.
It is the object that was created that gets 'deleted'.

Deleted means that the object gets it's destructor called if necessary, and the space it occupied is marked as free (to be used again) by the system.

NOTHING happens to the actual pointer, it will still point top that address, but of course now
it is meaningless.
Last edited on
thanks, so should we ever delete pointers themselves anyway? They would take up space too wouldn't they?
Yes, pointers will, just like normal variables, take up space. You can't delete them in C++, but the variables will be deleted at the end of the scope anyway.
ah, I see . Thanks. What if you had static pointers? wouldn't that cause issues?

and what's with the random int value displaying in my debugger anyway? I deleted my pointer to an new int object, how come it showed -17891602 int as the value?
Last edited on
http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx
http://msdn.microsoft.com/en-us/library/07x6b05d(VS.80).aspx

That could be because your debugger is showing the address. Either that, or something grabbed the memory space for its own.

-Albatross
Last edited on
Hi, I've been reading through those two links and I can't really see an answer to my question :S

I asked: ah, I see . Thanks. What if you had static pointers? wouldn't that cause issues?

Something static will "stay around" until the program exits right? So, if you had too many static pointers, this could be a problem? Even if you're deleting the objects that were created?

And if you delete the object that a pointer was pointing to, is the pointer now known as an "invalid pointer"?

Thanks!
Topic archived. No new replies allowed.