Computational Thinking for
Structured Design-II(303105151)
Mrs. Praptiba Solanki
Computer Science & Engineering Department (PIT)
CHAPTER-2
Dynamic Memory Allocation
Contents
• Introduction
• merits of DMA
• Malloc
• Calloc
• realloc and free functions
Dynamic memory allocation
• 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.
Dynamic memory allocation
• Here, length (size) of the array above is 9. But what if there is a
requirement to change this length (size)?
• For example, If 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. 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 indices more 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.
Dynamic Memory Allocation
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.
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. They are:
1.malloc()
2.calloc()
3.free()
4.realloc()
Merits of DMA
1.Allocation of memory: In static memory allocation the variables are
allocated permanently hence a wastage of memory is occurred, But in
case of dynamic memory allocation the variables are get allocated until
the program unit gets inactive. Memory allocation occurs during runtime.
2.Efficiency: The efficiency of dynamic memory allocation is more than static
memory allocation and it is very flexible also.
3.Memory reusability: The previously used memory can also be reused in
dynamic memory allocation, a function named “free” is used to release
the memory for future use when the user need it.
4.Reallocation of memory: In static memory allocation after a memory is
allocated we can’t change its size. But in dynamic memory allocation
using the “realloc()” function we can change the memory block size as per
our requirement.
malloc() memory allocation
• 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.
malloc() memory allocation
malloc() memory allocation
malloc() memory allocation
malloc() memory allocation
malloc() memory allocation
calloc() memory allocation
“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:
1. It initializes each block with a default value ‘0’.
2. It has two parameters or arguments as compare to malloc().
calloc() memory allocation
Syntax:
calloc() memory allocation
calloc() memory allocation
calloc() memory allocation
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 wastage of memory by
freeing it.
• Syntax: free(ptr);
free() method
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.
• Syntax:
realloc() method
Programming Applications
• Operating System Development
• Embedded Systems and IoT Devices
• Compilers and Interpreters
• Database Systems
• Game Development and Graphics
• Network Programming and Drivers
www.paruluniversity.ac.in