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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is implementation specific, but when
mallocis called, the size of the allocated memory is kept somewhere (usually offset from the pointer itself). Whenfreeis called, it will use that stored size.This is exactly why you should only ever call
freeon a pointer that was returned bymalloc.