You are here:   HomeData StructuresBasic of Data StructureWhat is difference between malloc()/free() and new/delete
by Dinesh Thakur Category: Data Structure

The function is used to allocate contiguous blocks of memory of same size during the execution of the program. So, it may be applied for creating arrays of fundamental types as well as structures. The function malloc ( ) may also be used for the same purpose. The prototype of the function calloc () is given below.

void* calloc( size_t n, size_t m);

In the above code, the first unsigned number n represents the number of memory blocks to be created and the second unsigned number m represents the size of each memory block. As an example, examine the following code:

short* Ptrs = (short*) calloc (5, sizeof(short));

For the above code, function calloc ()allocates 5 blocks of memory each of size two bytes for storing 5 short integer numbers. The return value is assigned to Ptrs . It is the address of the first byte of the first block.

The test for availability of memory as illustrated above for the function malloc () should be

included for this function as well. Thus, for allocating memory for n structures of type students (described

above) the code may be written as below.

struct student *Pst;

Pst = (struct student*) calloc (size_t n, sizeof(struct student));

if( Pst ==NULL)

printf("Error in allocation of memory.");

exit(1);





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Search Content







Advance Courses



Basic Courses



Advertise with Us