I’m aware of the following:
- malloc
- calloc
- realloc
What are the differences between these? Why does malloc seem to be used almost exclusively? Are there behavioral differences between compilers?
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.
mallocallocates memory. The contents of the memory are left as-is (filled with whatever was there before).callocallocates memory and sets its contents to all-zeros.reallocchanges the size of an existing allocated block of memory, or copies the contents of an existing block of memory into a newly allocated block of the requested size, and then deallocates the old block.Obviously,
reallocis a special-case situation. If you don’t have an old block of memory to resize (or copy and deallocate), there’s no reason to use it. The reason thatmallocis normally used instead ofcallocis because there is a run-time cost for setting the memory to all-zeros and if you’re planning to immediately fill the memory with useful data (as is common), there’s no point in zeroing it out first.These functions are all standard and behave reliably across compilers.