You are here:

C++/calloc and malloc

Advertisement


Question
Difference between calloc and malloc
What happened if memory is allotted using new and freed using delete
Or
Alloted using calloc and freed using delete


Answer
malloc allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
calloc allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode.

malloc and free are C style and new and delete are C++ style. You should not mix them, as you will possibly get run-time error, because they use different memory management. So using new and delete is alright, but using calloc and delete is not.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sam

Expertise

You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.

Experience

I'm a professional programer in C++ and network/internet programming.

©2016 About.com. All rights reserved.