Ashish Yadav Ashish Yadav - 2 months ago 6
C++ Question

What does deleting a pointer mean?

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

Answer

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.