0% found this document useful (0 votes)
434 views2 pages

Memory Allocations in C PDF

The calloc function allocates memory for an array and initializes all bytes to zero. It takes two arguments: the number of elements and the size of each element. Calloc returns a pointer to the allocated memory. Unlike malloc, calloc initializes the memory to zeros, so it can be used to reserve space for dynamic arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
434 views2 pages

Memory Allocations in C PDF

The calloc function allocates memory for an array and initializes all bytes to zero. It takes two arguments: the number of elements and the size of each element. Calloc returns a pointer to the allocated memory. Unlike malloc, calloc initializes the memory to zeros, so it can be used to reserve space for dynamic arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like