Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete [] clarification 1

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
Can anybody point me the difference between:

char *var;
delete []var;

and
delete var;

I know in principle what the two are used for (delete array containers and elements, for example), but I've encountered delete[] used on character pointers and I am not sure why to use it like this.

Also, why delete [] works no NULL pointers - meaning, no crashes occur? Can anybody offer me an insight on this?

Thx in advance. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
The simple answer is that a character pointer *is* a pointer to an *array* of characters. And any time an allocated object is an array, you must use the []delete syntax to deallocate all the elements. Here's the MSDN guidance:

"The delete keyword deallocates a block of memory. The argument pointer must point to a block of memory previously allocated by the new operator. If pointer points to an array, place empty brackets before pointer."

Now for your second question... delete() is designed to be harmless if it's used on a null pointer. Here's a quote from MSDN:

"Using delete on a pointer to an object not allocated with new gives unpredictable results. You can, however, use delete on a pointer with the value 0. This provision means that, because new always returns 0 on failure, deleting the result of a failed new operation is harmless."
 
Ok, I knew that angle on char * as being a character array... But still, you can use
char *ptr =...
delete ptr;

as well, witout the [], with no problem whatsoever. Actually, I never use [] when freing char* pointers and never had problems with that.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
The difference between delete and delete[] is that delete will only free the first memory location pointed to, but delete[] will go through the entire block of allocated memory and free each individual element. The best example of this is when you dynamically allocate classes.

Say you write:

MyClass mc=new MyClass[10];

Then, if you say

delete mc;

only the first object MyClass will be freed. If you say instead

delete[] mc;

each one will be released.

You can verify this by writing a class that has a destructor that outputs something to the screen. If you don't use delete[], you'll only see one line of output. If you do, then you'll see one line for each instance of the class that you allocated.

It then follows logically that if you write

char *c=new char[10];

and then free it with

delete c;

you've just caused a memory leak of 9 bytes.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top