What does deleting a pointer mean?
Solution 1
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.
Solution 2
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
.
Solution 3
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; // call o->~Object(), then releases memory
o = NULL;
When new
and delete
are used with standard C types in C++ source they behave like malloc
and free
.
Solution 4
You can't "delete" a pointer variable, only set their value to NULL (or 0).
Ashish Yadav
I am a CS graduate from National Institute of Technology , Patna , India . I like programming in C and C++ with special interests in Designing efficient data structures and algorithms. I am a Member of Technical Staff at NetApp Contacts: [email protected] [email protected] [email protected] [email protected]
Updated on September 14, 2020Comments
-
Ashish Yadav over 3 years
Is deleting a pointer same as freeing a pointer (that allocates the memory)?
-
Francesco about 14 yearsWell, no it isn't, since it calls the destructor while free does not!
-
Will about 14 yearsFurthermore, 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.
-
Chris Thompson about 14 yearsAh my mistake, I was thinking of "free" in a more general, nonkeyword sense
-
logout about 14 years+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.
-
Ashish Yadav about 14 yearsso it all brings back to the difference between in memory management of new/delete and free/malloc ?
-
Ashish Yadav about 14 yearsa pointer pointing to NULL and a 'deleted' pointer are same or different?
-
Johannes Schaub - litb about 14 years@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 ownoperator new
andoperator delete
respectively). -
Johannes Schaub - litb about 14 years@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).
-
Ashish Yadav about 14 years@litb then what will happen if i try to delete a pointer pointing to null?
-
Johannes Schaub - litb about 14 years@ashish, nothing will happen. deleting a null pointer has no effect. See the C++ FAQ for more information about it: parashift.com/c++-faq-lite/freestore-mgmt.html
-
fredoverflow about 14 yearsNothing.
delete 0
is defined to be a no-op. -
quamrana about 14 years+1, but technically the compiler calls the destructor, then releases the memory, in that order.
-
Jack about 14 yearsYes, I didn't specify order.. my mistake, actually calling a destructor of a deallocated object doesn't make much sense..
-
fredoverflow almost 10 years@piyukr No,
delete
calls the destructor before it releases the memory.free
just releases the memory. Callingfree
on a pointer obtained fromnew
causes undefined behavior. -
U007D over 7 yearsThe order comes from your comment following delete o; (should be reversed).