0% found this document useful (0 votes)
28 views29 pages

Dynamic Memory Allocation

The document discusses dynamic memory allocation in C using functions such as malloc(), calloc(), free(), and realloc(). It explains the necessity of changing the size of arrays during runtime and provides details on how each function operates, including syntax and examples. The importance of freeing allocated memory to prevent wastage is also emphasized.

Uploaded by

Arpan Narula
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)
28 views29 pages

Dynamic Memory Allocation

The document discusses dynamic memory allocation in C using functions such as malloc(), calloc(), free(), and realloc(). It explains the necessity of changing the size of arrays during runtime and provides details on how each function operates, including syntax and examples. The importance of freeing allocated memory to prevent wastage is also emphasized.

Uploaded by

Arpan Narula
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/ 29

Dynamic Memory Allocation in C using malloc(),

calloc(), free() and realloc()

Course Instructor:
Dr. Suman Kumar Maji

IIT Patna Chapter-7 1 / 29


Introduction

Since C is a structured language, it has some fixed rules for


programming.
One of them includes changing the size of an array.
An array is a collection of items stored at contiguous memory
locations.

IIT Patna Chapter-7 2 / 29


continued...

As can be seen, the length (size) of the array above is 9.


But what if there is a requirement to change this length (size)? For
example,
Suppose there is a situation where only 5 elements are needed to be
entered in this array.
In this case, the remaining 4 indices are just wasting memory in this
array.
So there is a requirement to lessen the length (size) of the array from
9 to 5.
Take another situation.
In this, there is an array of 9 elements with all 9 indices filled.
But there is a need to enter 3 more elements in this array. In this
case, 3 more indices are required.
So the length (size) of the array needs to be changed from 9 to 12.
This procedure is referred to as Dynamic Memory Allocation in C.

IIT Patna Chapter-7 3 / 29


Dynamic Memory Allocation in C

Dynamic memory allocation using malloc(), calloc(), free(), and


realloc() is essential for efficient memory management in C.
Therefore, C Dynamic Memory Allocation can be defined as a
procedure in which the size of a data structure (like Array) is
changed during the runtime.
C provides some functions to achieve these tasks.
There are 4 library functions provided by C defined under
<stdlib.h> header file to facilitate dynamic memory allocation in C
programming.

IIT Patna Chapter-7 4 / 29


C malloc() method

The “malloc” or “memory allocation” method in C is used to


dynamically allocate a single large block of memory with the
specified size.
It returns a pointer of type void which can be cast into a pointer of
any form.
It doesn’t Initialize memory at execution time so that it has
initialized each block with the default garbage value initially.
Syntax of malloc() in C

IIT Patna Chapter-7 5 / 29


malloc() syntax

If space is insufficient, allocation fails and returns a NULL pointer.

IIT Patna Chapter-7 6 / 29


Example of malloc() in C

IIT Patna Chapter-7 7 / 29


continued...

IIT Patna Chapter-7 8 / 29


Output

IIT Patna Chapter-7 9 / 29


C calloc() method

1 “calloc” or “contiguous allocation” method in C is used to


dynamically allocate the specified number of blocks of memory of
the specified type. it is very much similar to malloc() but has two
different points and these are:
2 It initializes each block with a default value ‘0’.
3 It has two parameters or arguments as compare to malloc().

IIT Patna Chapter-7 10 / 29


Syntax of calloc() in C

IIT Patna Chapter-7 11 / 29


continued...

If space is insufficient, allocation fails and returns a NULL pointer.

IIT Patna Chapter-7 12 / 29


Example of calloc() in C

IIT Patna Chapter-7 13 / 29


continued...

IIT Patna Chapter-7 14 / 29


Output

IIT Patna Chapter-7 15 / 29


C free() method

“free” method in C is used to dynamically de-allocate the memory.


The memory allocated using functions malloc() and calloc() is not
de-allocated on their own.
Hence the free() method is used, whenever the dynamic memory
allocation takes place.
It helps to reduce the wastage of memory by freeing it.
Syntax of free() in C

free(ptr);

IIT Patna Chapter-7 16 / 29


continued...

IIT Patna Chapter-7 17 / 29


Example of free() in C

IIT Patna Chapter-7 18 / 29


continued...

IIT Patna Chapter-7 19 / 29


Output

IIT Patna Chapter-7 20 / 29


C realloc() method

“realloc” or “re-allocation” method in C is used to dynamically


change the memory allocation of a previously allocated memory.
In other words, if the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be used to dynamically
re-allocate memory.
re-allocation of memory maintains the already present value and new
blocks will be initialized with the default garbage value.

IIT Patna Chapter-7 21 / 29


Syntax of realloc() in C

If space is insufficient, allocation fails and returns a NULL pointer.

IIT Patna Chapter-7 22 / 29


Example of realloc() in C

IIT Patna Chapter-7 23 / 29


continued...

IIT Patna Chapter-7 24 / 29


continued...

IIT Patna Chapter-7 25 / 29


Output

IIT Patna Chapter-7 26 / 29


One another example for realloc() method is

IIT Patna Chapter-7 27 / 29


continued...

IIT Patna Chapter-7 28 / 29


Output

IIT Patna Chapter-7 29 / 29

You might also like