Sanjeev Kumar Dangi Sanjeev Kumar Dangi - 3 years ago 78
C Question

What is the difference between freeing the pointer and assigning it to NULL?

Could somebody tell me the difference between:

int *p;


int *p;

Answer Source

free will deallocate the memory that p points to - simply assigning it to NULL will not (and thus you will have a memory leak).

It is worth mentioning that it is good practice to assign your pointer to NULL AFTER the call to free, as this will prevent you from accidentally trying to access the freed memory (which is still possible, but absolutely should not be done).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download