To return the memory to the free store, you use the delete keyword. The delete operation frees up the memory allocated through new. Here’s how you’d free p_int:
delete p_int;
After deleting a pointer, it is a good idea to reset it to point to NULL again:
delete p_int; p_int = NULL;
It isn’t necessary that you do this, but once a pointer is deleted, you can’t read or write to the memory it was pointing to because it’s been returned to the free store. By setting the pointer to NULL, if your code does try to dereference the pointer after it is freed, you will find out immediately because the program will crash. It happens a lot, even to experienced programmers
This is much better than finding out later, when your program crashes or corrupts some user’s data.
Luca A. Tarrini

Recent Comments