How much memory would be freed if pointer is changed in C?

Un-deterministic. Cannot say. You need to supply the exact pointer which was returned by malloc().

Passing a pointer to free() which is not returned by malloc() [and family] is undefined behaviour.

As per your question, if the malloc()-ed pointer is stored in p, and you do p++ and call free(p);, then there p is not anymore a pointer returned by the calloc, malloc, or realloc function.

As per Chapter 7.20.3.2, c99 standard, 2nd paragraph

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

Leave a Comment