On Thu, Aug 12, 2010 at 5:54 PM, Jos Timanta Tarigan
<[email protected]> wrote:

> i got question regarding the term delete in c++. when i have a pointer eg:
>
> char* test
>
> which is filled with an array later on, and then at the end of my method i 
> call
> delete test;
> what actually happened? is it just destroying the pointer so it led to 
> 'nowhere'
> or it also free the memory? eg if i put values on the pointer
>
> test = "imaginethisisaverylongsequenceofarray"
>
> will deleting the test also delete the content hence free the memory so it can
> be used by other program?

It depends on how you allocated memory for you pointer. You can only
delete something that was created with new (just like using malloc()
and free()). If your pointer was statically allocated, like this:

char *test = "some text";

You can't delete it, but it will go out of scope at the end of the
enclosing block.

I don't think delete necessarily deletes the contents of the memory,
it removes the allocation so the pointer isn't pointing at anything
and makes the memory available again. Trying to re-access the memory
without reallocating is not advised :-)

-- 
Brett W. McCoy -- http://www.electricminstrel.com
------------------------------------------------------------------------
"In the rhythm of music a secret is hidden; If I were to divulge it,
it would overturn the world."
    -- Jelaleddin Rumi

Reply via email to