Your program is likely to crash: the free () operation is actually quite simple in C, but only works with the original allocated address.
A typical memory allocator works like this pseudo-code:
- ask for 64 bytes
- the allocator allocates 70 bytes (another 6 bytes)
- the first 2 bytes are set to "signature", a pattern recognized by the allocator to identify the memory allocated to it
- the next 4 bytes indicate the allocated size
- returns a pointer to the beginning of the 7th byte
Therefore, when you call free(ptr) , the allocator sends 6 bytes in front of the pointer to verify the signature. If he does not find the signature, he crashes :)
Gui13 source share