Possible Duplicate:
How do free and malloc work in C?
How does free know how many bytes of memory to be free'd when called in a program?
This is implementation specific, but when malloc
is called, the size of the allocated memory is kept somewhere (usually offset from the pointer itself). When free
is called, it will use that stored size.
This is exactly why you should only ever call free
on a pointer that was returned by malloc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With