Dynamic Memory Allocation
What is Allocation
• The process of reserving memory is called
allocation.
• The way to allocate memory depends on the
type of memory.
What is Allocation
• C has two types of memory:
– Static memory
– dynamic memory
Static Memory
• Static memory is memory that is reserved for
variables before the program runs.
• Allocation of static memory is also known
as compile time memory allocation.
Static Memory
• Static memory allocation occurs during
the compilation phase, before the program
runs.
• Examples of static memory allocation include
global variables, static local variables etc.
Static Memory
• C automatically allocates memory for every variable
when the program is compiled.
• For example, if you create an integer array of 20
students,
say int student[20];
• C will reserve space for 20 elements which is typically
80 bytes of memory (20 * 4)
Static Memory
• int student[20];
• Assume later we decide to store details only for 5
students, then, we are not able to change the size of
the array.
• We are left with unnecessary reserved memory.
Static Memory
We want better control of
allocated memory, what to do?
Lets use Dynamic Memory Allocation
Dynamic Memory
• Dynamic memory is memory that is allocated after the
program starts running.
• Allocation of dynamic memory can also be referred to
as runtime memory allocation.
Dynamic Memory
• Unlike with static memory, we have full control over
how much memory is being used at any time.
• We can write code to determine how much memory
you need and allocate it.
• Dynamic memory does not belong to a variable, it can
only be accessed with pointers.
Dynamic Memory
• To allocate dynamic memory, we can use the below
functions:
– malloc( )
– calloc( )
• To use the above functions, it is necessary to include
the <stdlib.h> header file before the main() function.
Dynamic Memory
• The malloc() and calloc() functions allocate some
memory and return a pointer to its address.
Dynamic Memory – malloc()
• The malloc() function has one parameter, size, which
specifies how much memory to allocate, measured in
bytes.
(cast-data-type *)malloc(size-in-bytes);
• Example, to allocate memory for an integer:
int *ptr = (int *)malloc(sizeof(int));
Dynamic Memory – malloc()
• Remember:
The data in the memory allocated by malloc() is
unpredictable. To avoid unexpected values,
make sure to write something into the memory
before reading it.
Dynamic Memory – malloc()
Point Static Memory Allocation Dynamic Memory Allocation
Memory At compile-time, cannot be
At runtime, can be modified.
Reservation Time modified.
compile-time memory
Known as runtime memory allocation.
allocation.
Allocation/ Cannot allocate or deallocate Can allocate and deallocate
Deallocation memory during runtime. memory during runtime.
Memory Space
Uses stack space. Uses heap space.
Used
Less efficient due to no More efficient due to
Efficiency and
reusability of memory while reusability and flexibility of
Flexibility
running. memory.
Dynamic Memory Allocation – malloc()
Point: Lets see the use of malloc() for the allocation of storage blocks
Point: Lets check the values allocated by malloc() to the block of storage