You should always pass in exactly the same pointer to free that you received from malloc (or realloc .) If you do not, the “undefined behavior”, which is an art term, means that you cannot rely on the behavior of the program for any in a predictable way. In this case, however, you should expect it to work immediately. (If you are unlucky, this will damage the memory, which will lead to a crash after a while or, even worse, to an incorrect output.)
The only way to partially free memory is to realloc with a smaller size, but it is only useful for trimming at the end and does not guarantee that the cut piece is available for some other allocation.
source share