You are on page 1of 3

9/1/2016

Difference Between Malloc and Calloc in C

Difference Between Malloc and Calloc in C


There are two major differences between lmalloc!and !calloc! in C programming language: first, in the number of arguments. The lmalloc()!takes a single
argument, while !calloc()! takess two. Second, alloc() does not initialize the memory allocated, while jcalloc()!initializes the allocated memory to ZERO.
Both lmallocJ and !calloc!are used in C language for dynamic memory allocation they obtain blocks of memory dynamically. Dynamic memory allocation is a
unique feature of C language that enables us to create data types and structures of any size and length suitable to our programs. Following are the major
differences and similarities between alloc and !callocJ in detail with their syntax and example usage.

Difference Between Malloc and Calloc


Differences between malloc and calloc
malloc

calloc

The name !malloc!stands for memory allocation.

The name !calloc! stands for contiguous allocation.

oid *malloc(size t n)! returns a pointer to bytes of uninitialized

!void *calloc(size t n, size t size)! returns a pointer to enough free

storage, or !NULL!if the request cannot be satisfied. If the space assigned by

space for an array of objects of the specified size, or NUL if the request

lmalloc()!is overrun, the results are undefined.

cannot be satisfied. The storage is initialized to zero.

lmalloc()!takes one argument that is, number of bytes.

!calloc()!take two arguments those are: number of blocks and size of each
block.

syntax of lmalloc()!:

syntax of !calloc()!:

oid *malloc(size t n);!

oid *calloc(size t n, size t size);!

Allocates bytes of memory. If the allocation succeeds, a void pointer to

Allocates a contiguous block of memory large enough to hold elements of

the allocated memory is returned. Otherwise !NULL!is returned.

!size!bytes each. The allocated region is initialized to zero.

http://cs-fundamentals.com/tech-interview/c/difference-between-malloc-and-calloc.php

1/3

9/1/2016

Difference Between Malloc and Calloc in C

!calloc! takes little longer than !malloc! because of the extra step of initializing
lmalloc! is faster than !calloc!.

the allocated memory by zero. However, in practice the difference in speed is


very tiny and not recognizable.

Similarities Between Malloc and Calloc


The pointer returned by l malloc! or !calloc! has the proper alignment for the object in question, but it must be cast into the appropriate type.
Proper alignment means the value of the returned address is guaranteed to be an even multiple of alignment. The value of alignment must be a power of two
and must be greater than or equal to the size of a word.
The lmalloc()!, lcalloc()! functions will fail if:
The physical limits of the system are exceeded by bytes of memory which cannot be allocated.
There is not enough memory available to allocate bytes of memory; but the application could try again later.

Syntax of Malloc
!void *malloc(size t n);!
Allocates bytes of memory. If the allocation succeeds, a void pointer to the allocated memory is returned. Otherwise INULL! is returned.
/* Allocate memory for an int. */
int *ptr
if (ptr
{

=
==

(int*) malloc(sizeof (int));


NULL)

printf("Could not allocate memory\n");


exit( -1);
}
else

printf("Memory allocated successfully.\n");


http://cs-fundamentals.com/tech-interview/c/difference-between-malloc-and-calloc.php

2/3

9/1/2016

Difference Between Malloc and Calloc in C

Syntax of Calloc
oid *calloc(size t n, size t size);!

Allocates a contiguous block of memory large enough to hold elements of siz bytes each. The allocated region is initialized to zero.

/* Allocating memory for an array of 10 elements of type int.*/


int *ptr = (int*) calloc(10 ,sizeof (int));
if (ptr == NULL)
{

printf("Could not allocate memory\n");


exit(-1);

else
printf("Memory allocated successfully.\n");
Hope you have enjoyed reading differences and similarities between malloc and calloc. Both functions in are used for dynamic memory allocation. Please do
write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Get Free Tutorials by Email
Email:

http://cs-fundamentals.com/tech-interview/c/difference-between-malloc-and-calloc.php

3/3

You might also like