Вы находитесь на странице: 1из 3

What is the difference between calloc() and malloc() ?

1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated
will be (number_of_elements * size).
malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like
calloc(...).
2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufcient 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.

SOFT SKILLS
o ASSESSMENT CENTERS
o COMMUNICATION CLINIC
o LEARNING RESOURCES
CAREER
o ADVANCED ACADEMIES
o CAREER EXPLORER
o LEARNING RESOURCES
LIBRARY
o BROWSE
SUPPORT
o FAQS
o TUTORIALS
o TAKE A TOUR
Skip to content
difference between malloc and calloc in c language (other than no of arguments)??
Answer1
There are two diferences.
First, is in the number of arguments. Malloc() takes a single argument (memory required in
bytes), while calloc() needs two arguments.
Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the
allocated memory to ZERO.

calloc() allocates a memory area, the length will be the product of its parameters.
calloc flls the memory with ZERO's and returns a pointer to frst byte. If it fails to locate
enough space it returns a NULL pointer.
Syntax: ptr_var=(cast_type *)calloc(no_of_blocks , size_of_each_block);
i.e. ptr_var=(type *)calloc(n,s);
malloc() allocates a single block of memory of REQUSTED SIZE and returns a
pointer to frst byte. If it fails to locate requsted amount of memory it returns a null pointer.
Syntax: ptr_var=(cast_type *)malloc(Size_in_bytes);
The malloc() function take one argument, which is the number of bytes to allocate, while the
calloc() function takes two arguments, one being the number of elements, and the other
being the number of bytes to allocate for each of those elements. Also, calloc() initializes the
allocated space to zeroes, while malloc() does not.
Calloc vs. Malloc
Diffen
Technology
Computers
Software
Programming
When calloc is used to allocate a bloc of memory! the allocated region is initiali"ed
to "eroes# $n contrast! malloc does not touch the contents of the allocated bloc of
memory! which means it contains garbage %alues# This could potentially be a security
ris because the contents of memory are unpredictable and programming errors may
result in a lea of these contents#
Comparison chart
Differences Similarities
Calloc Malloc
Function
allocates a region of memory
large enough to hold &n
elements& of &si"e& bytes each#
'lso initiali"es contents of
memory to "eroes#
allocates &si"e&
bytes of
memory#
Calloc Malloc
Number of
arguments
( )
Syntax
%oid *calloc
(number+of+blocs!
si"e+of+each+bloc+in+bytes),
%oid *malloc
(si"e+in+bytes),
Contents
of
allocated
memory
The allocated region is
initiali"ed to "ero#
The contents of
allocated
memory are not
changed# i#e#!
the memory
contains
unpredictable
or garbage
%alues# This
presents a ris#
Return
value
%oid pointer (%oid *)# $f the
allocation succeeds! a pointer
to the bloc of memory is
returned# $f the allocation of
memory fails! a -.// pointer
is returned#
%oid pointer
(%oid *)# $f the
allocation
succeeds! a
pointer to the
bloc of
memory is
returned# $f the
allocation of
memory fails! a
-.// pointer is
returned#
malloc() and calloc() function
<<Previous Next>>
C - malloc() and calloc() function - August 06, 2008 at 13:10 PM by Amit at!ut"
Explain the difference between malloc() and calloc() function.
Both functions are used to dynamically allocate the memory. The difference is that calloc initializes the allocated
memory to 0 or Null while malloc contains garbage values.
C - malloc() and calloc() function - #an 11, 2010 at 16:16 PM by $idya aga%
Explain the difference between malloc() and calloc() function.
The following are the differences between malloc() and calloc():
- Byte of memory is allocated by malloc() whereas bloc! of memory is allocated by calloc().
- malloc() ta!es a single argument the size of memory where as calloc ta!es two "arameters the number of variables to allocate memory and size of bytes of a single variable
- #emory initialization is not "erformed by malloc() whereas memory is initialized by calloc().
- malloc(s) returns a "ointer with enough storage with s bytes where as calloc(ns) returns a "ointer with enough contiguous storage each with s bytes.