0% found this document useful (0 votes)
30 views3 pages

Calloc& Malloc

Malloc() and calloc() are functions used for dynamic memory allocation in C. Malloc() allocates memory and takes a single argument for the number of bytes, while calloc() allocates memory, initializes it to zeros, and takes two arguments for the number of elements and size of each element. The key difference is that malloc() returns uninitialized memory that may contain garbage values, while calloc() returns initialized memory of all zeros.

Uploaded by

Saikat Payra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
30 views3 pages

Calloc& Malloc

Malloc() and calloc() are functions used for dynamic memory allocation in C. Malloc() allocates memory and takes a single argument for the number of bytes, while calloc() allocates memory, initializes it to zeros, and takes two arguments for the number of elements and size of each element. The key difference is that malloc() returns uninitialized memory that may contain garbage values, while calloc() returns initialized memory of all zeros.

Uploaded by

Saikat Payra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Differences between malloc() and calloc()

Thursday, 01 September 2011 16:07 -

Allocation of memory at the time of execution is called dynamic memory allocation. It is done using the standard

library functions malloc() and calloc(). It is defined in "stdlib.h".

malloc(): used to allocate required number of bytes in memory at runtime. It takes one argument, viz. size in

bytes to be allocated.

Syntax:

void * malloc(size_t size);

Example:

a = (int*) malloc(4);

4 is the size (in bytes) of memory to be allocated.

calloc(): used to allocate required number of bytes in memory at runtime. It needs two arguments

vi

1/3

Differences between malloc() and calloc()


Thursday, 01 September 2011 16:07 -

z.,

1. total number of data and

2. size of each data.

Syntax:

void * calloc(size_t nmemb, size_t size);

Example:

a = (int*) calloc(8, sizeof(int));

Here sizeof indicates the size of the data type and 8 indicates that we want to reserve space for storing 8

integers.

Difference is.

1. Number of arguments differ.

2/3

Differences between malloc() and calloc()


Thursday, 01 September 2011 16:07 -

2. By default, memory allocated by malloc() contains garbage values. Whereas memory allocated by calloc() contains all zeros.

3/3

You might also like