views:

311

answers:

7

Is deleting a pointer same as freeing a pointer (that deallocates the memory)?

A: 

Yes and it calls the appropriate destructor.

Chris Thompson
Well, no it isn't, since it calls the destructor while free does not!
Francesco
Furthermore, there's no guarantee that "delete" and "free" work off the same heaps. If you allocate something with "new" and then do a "free" on it, you may corrupt your heap and/or crash your program. Likewise with the "malloc"/"delete" combo.
Will
Ah my mistake, I was thinking of "free" in a more general, nonkeyword sense
Chris Thompson
+1  A: 

You can't "delete" a pointer variable, only set their value to NULL (or 0).

Alen Sindicic
+1. This is the correct answer - if Ashish meant a C pointer such as when defining "char *cpString". In C, we never free a pointer, but instead we free allocated memory.
logout
thats why i tagged it to c++
CadetNumber1
+6  A: 

Deleting a pointer (or deleting what it points to, alternatively) means

delete p;
delete[] p; // for arrays

p was allocated prior to that statement like

p = new type;

It may also refer to using other ways of dynamic memory management, like free

free(p);

which was previously allocated using malloc or calloc

p = malloc(size);

The latter is more often referred to as "freeing", while the former is more often called "deleting". delete is used for classes with a destructor since delete will call the destructor in addition to freeing the memory. free (and malloc, calloc etc) is used for basic types, but in C++ new and delete can be used for them likewise, so there isn't much reason to use malloc in C++, except for compatibility reasons.

Johannes Schaub - litb
so it all brings back to the difference between in memory management of new/delete and free/malloc ?
CadetNumber1
a pointer pointing to NULL and a 'deleted' pointer are same or different?
CadetNumber1
@ashish, yes. malloc and free do only care about the memory, not about the type of what will be in it. `new` cares about the type and calls constructors if necessary, and it can also be extended to get memory from a different memory pool than the default (by writing your own `operator new` and `operator delete` respectively).
Johannes Schaub - litb
@ashish, changing a pointer's value to null is different from deleting it. The pointer will point to no object afterwards if you only set it to null, but the memory of the object it pointed to previously won't be freed. So in effect you will leak memory by doing that. First delete/free, and then set to null (don't let deleted pointers keep their old value - it will make you unable to test later whether the pointer points to allocated memory or not. That's bad).
Johannes Schaub - litb
@litb then what will happen if i try to delete a pointer pointing to null?
CadetNumber1
@ashish, nothing will happen. deleting a null pointer has no effect. See the C++ FAQ for more information about it: http://www.parashift.com/c++-faq-lite/freestore-mgmt.html
Johannes Schaub - litb
Nothing. `delete 0` is defined to be a no-op.
FredOverflow
oh thanks really
CadetNumber1
A: 

In short, yes.

But you have to be careful: if you allocate with p = new sometype() only then should you use delete p. If you allocate using p = sometype[count] always use delete [] p

And one more thing: you should never pair malloc/delete or new/free.

shinjin
+2  A: 

Yes, delete is used to deallocate memory and call the destructor for the object involved.

It's common pratice to set pointer to NULL after deleting it to avoid having invalid pointers around:

Object *o = new Object();

// use object
delete o; // releases memory and call o->~Object()
o = NULL;

When new and delete are used with standard C types in C++ source they behave like malloc and free.

Jack
+1, but technically the compiler calls the destructor, then releases the memory, in that order.
quamrana
Yes, I didn't specify order.. my mistake, actually calling a destructor of a deallocated object doesn't make much sense..
Jack
+1  A: 

Yes deleting a pointer is the same as deallocating memory or freeing memory, etc.

+2  A: 

You can't "delete" a pointer variable

Sure you can ;-)

int** p = new int*(new int(42));
delete *p;
delete p; // <--- deletes a pointer

But seriously, delete should really be called delete_what_the_following_pointer_points_to.

FredOverflow