How are we able to access the pointer after deallocating the memory?

You can certainly continue to use p after calling free(p) and nothing will stop you. However the results will be completely undefined and unpredictable. It works by luck only. This is a common programming error called “use after free” which works in many programs for literally years without “problems” — until it causes a problem.

There are tools which are quite good at finding such errors, such as Valgrind.

Leave a Comment