What does deleting a pointer mean?

27,794

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).

Share:
27,794
Ashish Yadav
Author by

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, 2020

Comments

  • Ashish Yadav
    Ashish Yadav over 3 years

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

  • Francesco
    Francesco about 14 years
    Well, no it isn't, since it calls the destructor while free does not!
  • Will
    Will about 14 years
    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.
  • Chris Thompson
    Chris Thompson about 14 years
    Ah my mistake, I was thinking of "free" in a more general, nonkeyword sense
  • logout
    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
    Ashish Yadav about 14 years
    so it all brings back to the difference between in memory management of new/delete and free/malloc ?
  • Ashish Yadav
    Ashish Yadav about 14 years
    a pointer pointing to NULL and a 'deleted' pointer are same or different?
  • Johannes Schaub - litb
    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 own operator new and operator delete respectively).
  • Johannes Schaub - litb
    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
    Ashish Yadav about 14 years
    @litb then what will happen if i try to delete a pointer pointing to null?
  • Johannes Schaub - litb
    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
    fredoverflow about 14 years
    Nothing. delete 0 is defined to be a no-op.
  • quamrana
    quamrana about 14 years
    +1, but technically the compiler calls the destructor, then releases the memory, in that order.
  • Jack
    Jack about 14 years
    Yes, I didn't specify order.. my mistake, actually calling a destructor of a deallocated object doesn't make much sense..
  • fredoverflow
    fredoverflow almost 10 years
    @piyukr No, delete calls the destructor before it releases the memory. free just releases the memory. Calling free on a pointer obtained from new causes undefined behavior.
  • U007D
    U007D over 7 years
    The order comes from your comment following delete o; (should be reversed).