4/27/2014 calloc c | calloc function in c | in c programming language
Share
Share
Share
Share
More35
Introduction calloc() function
Turbo C++ IDE The calloc function is used to allocate storage to a
A Basic C Program variable while the program is running. This library function is
Fundamentals invoked by writing calloc(num,size).This function takes two
Input/Output Functions arguments that specify the number of elements to be reserved,
and the size of each element in bytes and it allocates memory
Control Statements
block equivalent to num * size . The function returns a pointer
Arrays
to the beginning of the allocated storage area in memory. The
Strings
important difference between malloc and calloc function is that
Functions
calloc initializes all bytes in the allocation block to zero and the
Storage Classes allocated memory may/may not be contiguous.
Structure
calloc function is used to reserve space for dynamic arrays. Has
Union
the following form.
Pointers
Dynamic memory allocation void * calloc (size_t n, size_t size);
File
Number of elements in the first argument specifies the size in
Graphics bytes of one element to the second argument. A successful
The C Preprocessor partitioning, that address is returned, NULL is returned on
Standard Library Functions failure.
ASCII Table
For example, an int array of 10 elements can be allocated as
Examples follows.
Questions and Answers int * array = (int *) calloc (10, sizeof (int));
Note that this function can also malloc, written as follows.
int * array = (int *) malloc (sizeof (int) * 10);
However, the malloc function, whereas the area reserved to
the states that are undefined, the area allocated by the calloc
function contains a 0. In fact, the calloc function is internally
may be a function that calls malloc. After securing function by
malloc, the area is filled with 0.
ptr = malloc(10 * sizeof(int)); is just like this:
http://www.cprogrammingexpert.com/C/Tutorial/dynamic_memmory_allocation/calloc.aspx 1/2
4/27/2014 calloc c | calloc function in c | in c programming language
ptr = calloc(10, sizeof(int));
The following example illustrates the use of
calloc() function
.
When you release the space allocated by calloc
function, use the free function of course.
malloc Home realloc
http://www.cprogrammingexpert.com/C/Tutorial/dynamic_memmory_allocation/calloc.aspx 2/2