SlideShare a Scribd company logo
Dynamic Memory Allocation
Memory of a Program
Memory
assigned to
a program
Whenever a program
executes, the operating
system allocates some space
in memory for it.
Memory of a Program
Heap
Stack
Static/Global
Code Instructions
Global variables
Function calls
& Local variables
The memory allocated to a
program is divided into four
parts.
Memory of a Program
Heap
Stack
Static/Global
Code
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
Stack frame for
main() function
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
SquareOfSum()
x, y, z
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
SquareOfSum()
x, y, z
Square()
x
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
SquareOfSum()
x, y, z
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
printf()
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
total
main()
a, b
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
Use of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
LIFO – Last In First Out
Limitation of Stack
#include<stdio.h>
int total;
int Square(int x)
{
return(x*x);
}
int SquareOfSum(int x,int y)
{
int z = Square(x+y);
return(z);
}
int main()
{
int a=4, b=8;
total = SquareOfSum(a,b);
printf(“Output=%d”,total);
}
Heap
Stack
Static/
Global
Code
Stack Overflow
FuncA()
FuncB()
FuncC()
FuncD()
Limitation of Stack
• Allocation and deallocation of stack memory are handled by
Operating System. Programmer cannot control the lifetime of variables
in stack memory.
• Allocation: Function starts (Push onto stack)
• Deallocation: Function finishes (Popped out of stack)
• Size of the stack frame for a function is known at the compile time.
• An array with unknown size (only known at run time) cannot be
allocated in the stack memory.
• For this, we need to use heap memory.
Heap Memory
• A programmer can define the size of an array at run time
using dynamic memory allocation.
• All variables/array defined using dynamic memory allocation
are allocated memory from heap.
• The programmer can decide how long the allocated memory
to be kept.
• Heap can grow as long as the program does not run out of the
memory allocated to it.
– Sometimes dangerous if proper care is not taken.
• Heap is called a free pool/store of memory
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language
• malloc() – Allocates block of memory
• calloc() – Allocate multiple blocks and
initialize to 0.
• realloc() – Reallocates block of
memory
• free() – Frees up memory
C++ language
• new – Allocates block of memory
• delete – Frees up memory
Heap
Stack
Static/
Global
Code
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language (Example code)
#include<stdio.h>
#include<stdlib.h>
Heap
Stack
Static/
Global
Code
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
Heap
Stack
Static/
Global
Code
main()
a 10
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = (int*)malloc(sizeof(int));
Heap
Stack
Static/
Global
Code
main()
a 10
p 200
? 200
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = (int*)malloc(sizeof(int));
*p=15;
Heap
Stack
Static/
Global
Code
main()
a 10
p 200
15 200
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = (int*)malloc(sizeof(int));
*p=15;
p=(int*)malloc(sizeof(int));
*p=20;
Heap
Stack
Static/
Global
Code
main()
a 10
p 100
15 200
20 100
Memory
Leak
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = (int*)malloc(sizeof(int));
*p=15;
free(p);
p=(int*)malloc(sizeof(int));
*p=20;
}
Heap
Stack
Static/
Global
Code
main()
a 10
p 100
200
20 100
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = (int*)malloc(4*sizeof(int));
p[0]=10;
p[1]=20;
*(p+2)=30;
*(p+3)=40;
}
Heap
Stack
Static/
Global
Code
main()
a 10
p 150
10 150
20
30
40
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C++ language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = new int;
*p=10;
Heap
Stack
Static/
Global
Code
main()
a 10
p 200
10 200
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C++ language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = new int;
*p=10;
delete p;
Heap
Stack
Static/
Global
Code
main()
a 10
200
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C++ language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = new int;
*p=10;
delete p;
p = new int[4];
for(int i=0;i<4;i++)
p[i]=0;
Heap
Stack
Static/
Global
Code
main()
a 10
50
p 50
0
0
0
0
Use of Heap
(Dynamic Memory Allocation)
Dynamic Memory Allocation
C++ language (Example code)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=10;
int *p;
p = new int;
*p=10;
delete p;
p = new int[4];
for(int i=0;i<4;i++)
p[i]=0;
delete[ ] p;
}
Heap
Stack
Static/
Global
Code
main()
a 10
50
Dynamic 2D Array Allocation in C
Using single pointer
Dynamic 2D Array Allocation in C
Using single pointer
main()
arr 200
200
Space for
r x c
elements
(i,j)th Element: *(arr+i*c+j) or arr[i*c+j]
Stack
Heap
2D array mapped into 1D array in memory
Dynamic 2D Array Allocation in C
Using array of pointers
Dynamic 2D Array Allocation in C
Using single pointer
main()
arr 300
100
Space for c
elements of
0th row
(i,j)th Element: arr[i][j]
300
600
Space for c
elements of
0th row
Space for c
elements of
0th row
arr[2] 100
arr[1] 800
arr[0] 600
800
Stack
Heap
Freeing Dynamic 2D Array in C
• For single pointer:
main()
arr 200
200
Space for
r x c
elements
Stack
Heap
Freeing Dynamic 2D Array in C
• For single pointer:
free(arr);
main()
200
Stack
Heap
Freeing Dynamic 2D Array in C
• For array of pointers
main()
arr 300
100
Space for c
elements of
0th row
300
600
Space for c
elements of
0th row
Space for c
elements of
0th row
arr[2] 100
arr[1] 800
arr[0] 600
800
Stack
Heap
Freeing Dynamic 2D Array in C
• For array of pointers
for(i=0;i<r;i++){
free(arr[i]);}
main()
arr 300
100
300
600
800
Stack
Heap
Freeing Dynamic 2D Array in C
• For array of pointers
for(i=0;i<r;i++){
free(arr[i]);}
free(arr);
main()
100
300
600
800
Stack
Heap
Dynamic 2D Array Allocation and Freeing in C++
Using single pointer
int r=3, c=4;
int** arr = new int[r*c];
(i,j)th element can be accessed by *(arr + i*c+j) or arr[i*c+j]
For freeing memory:
delete[] arr;
Dynamic 2D Array Allocation and Freeing in C++
Using array of pointers
int r=3, c=4;
int** arr = new int*[r];
for(i=0;i<r;i++)
arr[i] = new int[c];
(i,j)th element can be accessed by arr[i][j];
For freeing memory:
for(i=0;i<r;i++){
delete[] arr[i]);}
delete[] arr;
arr
arr[0]
arr[1]
arr[2]
Limitations of Dynamic Array
200
201
202
Heap Memory
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
201
202
Limitations of Dynamic Array
• Memory Manager (A part of operating systems)
manages the memory.
• Keeps track of free space
• Allocates space on request from the programs
Heap Memory (Horizontal View)
200
201
202
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
201
202
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int));
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
213
220
q
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
213
220
q
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
213
220
q
Limitations of Dynamic Array
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
213
220
q
221
236
r
Heap Memory (Horizontal View)
Limitations of Dynamic Array
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
free(q)
213
220
q
221
236
r
Heap Memory (Horizontal View)
Limitations of Dynamic Array
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
free(q)
213
220
221
236
r
Heap Memory (Horizontal View)
Limitations of Dynamic Array
200
201
202
212
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
free(q)
int *s=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
Although free memory is more than required 3 x 4 = 12 bytes, yet it
cannot be allocated as it is not contiguous. Returns a null pointer.
213
220
221
236
r
Heap Memory (Horizontal View)
Solution: Linked List
200
201
202
212
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
Instead of asking memory manager for an integer array of 4 elements,
these 4 integers can be stored one at a time in memory in different
places.
213
220
221
236
Heap Memory (Horizontal View)
7
Solution: Linked List
200
201
202
212
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
int *p=(int*)malloc(sizeof(int)); *p=7;
213
220
221
236
Heap Memory (Horizontal View)
p
10
7
Solution: Linked List
200
201
202
212
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
int *p=(int*)malloc(sizeof(int)); *p=7;
int *q=(int*)malloc(sizeof(int)); *q=10;
213
222
236
Heap Memory (Horizontal View)
p q
5 10
7
Solution: Linked List
200
201
202
212
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
int *p=(int*)malloc(sizeof(int)); *p=7;
int *q=(int*)malloc(sizeof(int)); *q=10;
int *r=(int*)malloc(sizeof(int)); *r=5;
213
222
236
Heap Memory (Horizontal View)
p q
r
9
5 10
7
Solution: Linked List
200
201
202
212
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
int *p=(int*)malloc(sizeof(int)); *p=7;
int *q=(int*)malloc(sizeof(int)); *q=10;
int *r=(int*)malloc(sizeof(int)); *r=5;
int *s=(int*)malloc(sizeof(int)); *s=9;
Non-contiguous allocation: Hence it is not possible to traverse the list
directly as done in array.
213
222
233
Heap Memory (Horizontal View)
p q
r s
9
5 10
7
Solution: Linked List
200
201
202
212
To traverse the elements of the list, one should store the address of the
next element in the list with each element.
213
222
233
Heap Memory (Horizontal View)
p q
r s
-1
213
233
222 9
5 10
7
Solution: Linked List
202
To traverse the elements of the list {7, 10, 5, 9}, one should store the
address of the next element (pointer to next element) in the list with
each element.
It is sufficient to know the address of (pointer to) the first node (also
called as head node) to retrieve all the elements of the list.
The last node points to null (represented by -1 here).
213
222
233
Heap Memory (Horizontal View)
p
Logical View of Linked List
7 10 5 9
Head
Structure of each node
struct node
{
int val;
struct node* next;
}

More Related Content

Similar to Dynamic memory allocation for data structure DS3a.pdf (20)

PPTX
Dynamic Memory allocation
Grishma Rajput
 
PPTX
Functions using stack and heap
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Memory Management.pptx
BilalImran17
 
PPTX
final GROUP 4.pptx
ngonidzashemutsipa
 
PPTX
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
PPTX
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
AkhilMishra50
 
PPTX
Lecture 15 run timeenvironment_2
Iffat Anjum
 
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
PPTX
Dynamic Memory Allocation.pptx
ssuser688516
 
PPTX
Functions with heap and stack
baabtra.com - No. 1 supplier of quality freshers
 
PDF
06 linked list
Rajan Gautam
 
PPT
Stack and heap allocation
ankitbhasin23
 
PPTX
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
PPTX
1. C Basics for Data Structures Bridge Course
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
DOCX
Dma
Acad
 
PDF
Programming in C language specially allocation
DarkVibes4
 
PPT
GLA University is inviting you to a scheduled Zoom meeting
Arun Kumar
 
PPT
Dynamic allocation
CGC Technical campus,Mohali
 
Dynamic Memory allocation
Grishma Rajput
 
Functions using stack and heap
baabtra.com - No. 1 supplier of quality freshers
 
Memory Management.pptx
BilalImran17
 
final GROUP 4.pptx
ngonidzashemutsipa
 
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
AkhilMishra50
 
Lecture 15 run timeenvironment_2
Iffat Anjum
 
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
Dynamic Memory Allocation.pptx
ssuser688516
 
06 linked list
Rajan Gautam
 
Stack and heap allocation
ankitbhasin23
 
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
Dma
Acad
 
Programming in C language specially allocation
DarkVibes4
 
GLA University is inviting you to a scheduled Zoom meeting
Arun Kumar
 
Dynamic allocation
CGC Technical campus,Mohali
 

Recently uploaded (20)

PPTX
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PDF
Choosing the Right Database for Indexing.pdf
Tamanna
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PPT
Data base management system Transactions.ppt
gandhamcharan2006
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
PPTX
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
PPTX
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PDF
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PDF
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
Choosing the Right Database for Indexing.pdf
Tamanna
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
Data base management system Transactions.ppt
gandhamcharan2006
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
Ad

Dynamic memory allocation for data structure DS3a.pdf

  • 2. Memory of a Program Memory assigned to a program Whenever a program executes, the operating system allocates some space in memory for it.
  • 3. Memory of a Program Heap Stack Static/Global Code Instructions Global variables Function calls & Local variables The memory allocated to a program is divided into four parts.
  • 4. Memory of a Program Heap Stack Static/Global Code #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); }
  • 5. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code
  • 6. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total
  • 7. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b Stack frame for main() function
  • 8. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b
  • 9. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b SquareOfSum() x, y, z
  • 10. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b SquareOfSum() x, y, z Square() x
  • 11. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b SquareOfSum() x, y, z
  • 12. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b
  • 13. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b printf()
  • 14. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code total main() a, b
  • 15. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code
  • 16. Use of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code LIFO – Last In First Out
  • 17. Limitation of Stack #include<stdio.h> int total; int Square(int x) { return(x*x); } int SquareOfSum(int x,int y) { int z = Square(x+y); return(z); } int main() { int a=4, b=8; total = SquareOfSum(a,b); printf(“Output=%d”,total); } Heap Stack Static/ Global Code Stack Overflow FuncA() FuncB() FuncC() FuncD()
  • 18. Limitation of Stack • Allocation and deallocation of stack memory are handled by Operating System. Programmer cannot control the lifetime of variables in stack memory. • Allocation: Function starts (Push onto stack) • Deallocation: Function finishes (Popped out of stack) • Size of the stack frame for a function is known at the compile time. • An array with unknown size (only known at run time) cannot be allocated in the stack memory. • For this, we need to use heap memory.
  • 19. Heap Memory • A programmer can define the size of an array at run time using dynamic memory allocation. • All variables/array defined using dynamic memory allocation are allocated memory from heap. • The programmer can decide how long the allocated memory to be kept. • Heap can grow as long as the program does not run out of the memory allocated to it. – Sometimes dangerous if proper care is not taken. • Heap is called a free pool/store of memory
  • 20. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language • malloc() – Allocates block of memory • calloc() – Allocate multiple blocks and initialize to 0. • realloc() – Reallocates block of memory • free() – Frees up memory C++ language • new – Allocates block of memory • delete – Frees up memory Heap Stack Static/ Global Code
  • 21. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language (Example code) #include<stdio.h> #include<stdlib.h> Heap Stack Static/ Global Code
  • 22. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; Heap Stack Static/ Global Code main() a 10
  • 23. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = (int*)malloc(sizeof(int)); Heap Stack Static/ Global Code main() a 10 p 200 ? 200
  • 24. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = (int*)malloc(sizeof(int)); *p=15; Heap Stack Static/ Global Code main() a 10 p 200 15 200
  • 25. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = (int*)malloc(sizeof(int)); *p=15; p=(int*)malloc(sizeof(int)); *p=20; Heap Stack Static/ Global Code main() a 10 p 100 15 200 20 100 Memory Leak
  • 26. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = (int*)malloc(sizeof(int)); *p=15; free(p); p=(int*)malloc(sizeof(int)); *p=20; } Heap Stack Static/ Global Code main() a 10 p 100 200 20 100
  • 27. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = (int*)malloc(4*sizeof(int)); p[0]=10; p[1]=20; *(p+2)=30; *(p+3)=40; } Heap Stack Static/ Global Code main() a 10 p 150 10 150 20 30 40
  • 28. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C++ language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = new int; *p=10; Heap Stack Static/ Global Code main() a 10 p 200 10 200
  • 29. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C++ language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = new int; *p=10; delete p; Heap Stack Static/ Global Code main() a 10 200
  • 30. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C++ language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = new int; *p=10; delete p; p = new int[4]; for(int i=0;i<4;i++) p[i]=0; Heap Stack Static/ Global Code main() a 10 50 p 50 0 0 0 0
  • 31. Use of Heap (Dynamic Memory Allocation) Dynamic Memory Allocation C++ language (Example code) #include<stdio.h> #include<stdlib.h> int main() { int a=10; int *p; p = new int; *p=10; delete p; p = new int[4]; for(int i=0;i<4;i++) p[i]=0; delete[ ] p; } Heap Stack Static/ Global Code main() a 10 50
  • 32. Dynamic 2D Array Allocation in C Using single pointer
  • 33. Dynamic 2D Array Allocation in C Using single pointer main() arr 200 200 Space for r x c elements (i,j)th Element: *(arr+i*c+j) or arr[i*c+j] Stack Heap 2D array mapped into 1D array in memory
  • 34. Dynamic 2D Array Allocation in C Using array of pointers
  • 35. Dynamic 2D Array Allocation in C Using single pointer main() arr 300 100 Space for c elements of 0th row (i,j)th Element: arr[i][j] 300 600 Space for c elements of 0th row Space for c elements of 0th row arr[2] 100 arr[1] 800 arr[0] 600 800 Stack Heap
  • 36. Freeing Dynamic 2D Array in C • For single pointer: main() arr 200 200 Space for r x c elements Stack Heap
  • 37. Freeing Dynamic 2D Array in C • For single pointer: free(arr); main() 200 Stack Heap
  • 38. Freeing Dynamic 2D Array in C • For array of pointers main() arr 300 100 Space for c elements of 0th row 300 600 Space for c elements of 0th row Space for c elements of 0th row arr[2] 100 arr[1] 800 arr[0] 600 800 Stack Heap
  • 39. Freeing Dynamic 2D Array in C • For array of pointers for(i=0;i<r;i++){ free(arr[i]);} main() arr 300 100 300 600 800 Stack Heap
  • 40. Freeing Dynamic 2D Array in C • For array of pointers for(i=0;i<r;i++){ free(arr[i]);} free(arr); main() 100 300 600 800 Stack Heap
  • 41. Dynamic 2D Array Allocation and Freeing in C++ Using single pointer int r=3, c=4; int** arr = new int[r*c]; (i,j)th element can be accessed by *(arr + i*c+j) or arr[i*c+j] For freeing memory: delete[] arr;
  • 42. Dynamic 2D Array Allocation and Freeing in C++ Using array of pointers int r=3, c=4; int** arr = new int*[r]; for(i=0;i<r;i++) arr[i] = new int[c]; (i,j)th element can be accessed by arr[i][j]; For freeing memory: for(i=0;i<r;i++){ delete[] arr[i]);} delete[] arr; arr arr[0] arr[1] arr[2]
  • 43. Limitations of Dynamic Array 200 201 202 Heap Memory
  • 44. Limitations of Dynamic Array Heap Memory (Horizontal View) 200 201 202
  • 45. Limitations of Dynamic Array • Memory Manager (A part of operating systems) manages the memory. • Keeps track of free space • Allocates space on request from the programs Heap Memory (Horizontal View) 200 201 202
  • 46. Limitations of Dynamic Array Heap Memory (Horizontal View) 200 201 202 Consider a program segment: int *p=(int*)malloc(3*sizeof(int));
  • 47. Limitations of Dynamic Array Heap Memory (Horizontal View) 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
  • 48. Limitations of Dynamic Array Heap Memory (Horizontal View) 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
  • 49. Limitations of Dynamic Array Heap Memory (Horizontal View) 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes 213 220 q
  • 50. Limitations of Dynamic Array Heap Memory (Horizontal View) 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes 213 220 q
  • 51. Limitations of Dynamic Array Heap Memory (Horizontal View) 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes 213 220 q
  • 52. Limitations of Dynamic Array 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes 213 220 q 221 236 r Heap Memory (Horizontal View)
  • 53. Limitations of Dynamic Array 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes free(q) 213 220 q 221 236 r Heap Memory (Horizontal View)
  • 54. Limitations of Dynamic Array 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes free(q) 213 220 221 236 r Heap Memory (Horizontal View)
  • 55. Limitations of Dynamic Array 200 201 202 212 p Consider a program segment: int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes free(q) int *s=(int*)malloc(3*sizeof(int)); // int size is 4 bytes Although free memory is more than required 3 x 4 = 12 bytes, yet it cannot be allocated as it is not contiguous. Returns a null pointer. 213 220 221 236 r Heap Memory (Horizontal View)
  • 56. Solution: Linked List 200 201 202 212 Suppose we need to store a list of 4 integers: 7, 10, 5, 9. Instead of asking memory manager for an integer array of 4 elements, these 4 integers can be stored one at a time in memory in different places. 213 220 221 236 Heap Memory (Horizontal View)
  • 57. 7 Solution: Linked List 200 201 202 212 Suppose we need to store a list of 4 integers: 7, 10, 5, 9. int *p=(int*)malloc(sizeof(int)); *p=7; 213 220 221 236 Heap Memory (Horizontal View) p
  • 58. 10 7 Solution: Linked List 200 201 202 212 Suppose we need to store a list of 4 integers: 7, 10, 5, 9. int *p=(int*)malloc(sizeof(int)); *p=7; int *q=(int*)malloc(sizeof(int)); *q=10; 213 222 236 Heap Memory (Horizontal View) p q
  • 59. 5 10 7 Solution: Linked List 200 201 202 212 Suppose we need to store a list of 4 integers: 7, 10, 5, 9. int *p=(int*)malloc(sizeof(int)); *p=7; int *q=(int*)malloc(sizeof(int)); *q=10; int *r=(int*)malloc(sizeof(int)); *r=5; 213 222 236 Heap Memory (Horizontal View) p q r
  • 60. 9 5 10 7 Solution: Linked List 200 201 202 212 Suppose we need to store a list of 4 integers: 7, 10, 5, 9. int *p=(int*)malloc(sizeof(int)); *p=7; int *q=(int*)malloc(sizeof(int)); *q=10; int *r=(int*)malloc(sizeof(int)); *r=5; int *s=(int*)malloc(sizeof(int)); *s=9; Non-contiguous allocation: Hence it is not possible to traverse the list directly as done in array. 213 222 233 Heap Memory (Horizontal View) p q r s
  • 61. 9 5 10 7 Solution: Linked List 200 201 202 212 To traverse the elements of the list, one should store the address of the next element in the list with each element. 213 222 233 Heap Memory (Horizontal View) p q r s
  • 62. -1 213 233 222 9 5 10 7 Solution: Linked List 202 To traverse the elements of the list {7, 10, 5, 9}, one should store the address of the next element (pointer to next element) in the list with each element. It is sufficient to know the address of (pointer to) the first node (also called as head node) to retrieve all the elements of the list. The last node points to null (represented by -1 here). 213 222 233 Heap Memory (Horizontal View) p
  • 63. Logical View of Linked List 7 10 5 9 Head Structure of each node struct node { int val; struct node* next; }