SlideShare a Scribd company logo
9
Most read
10
Most read
12
Most read
Dynamic Memory Allocation
Subject : C Language
Name : Uttam Verma
SYNOPSIS
• Memory allocation
• Static MemoryAllocation
• Memory Allocation Process
• Memory Allocation Functions
• Allocation A Block Of Memory :Malloc
• Allocation A Block Of Memory :Calloc
• Altering The Size Of A Block :Realloc
• Releasing The Used Space: Free
Dynamic memory allocation
MEMORYALLOCATION
• The blocks of information in a memory system is called memory
allocation.
• To allocate memory it is necessary to keep in information of
available memory in the system. If memory management system
finds sufficient free memory, it allocates only as much memory as
needed, keeping the rest available to satisfy future request.
• Memory allocation has two types. They are static and dynamic
memory allocation.
STATIC MEMORY ALLOCATION
• In static memory allocation, size of the memory may be required
and that must be defined before loading and executing the program.
DYNAMIC MEMORYALLOCATION
• In the dynamic memory allocation, the memory is allocated to a
variable or program at the run time.
• The only way to access this dynamically allocated memory is
through pointer.
MEMORY ALLOCATION FUNCTIONS
ALLOCATION A BLOCK OF MEMORY:
MALLOC
malloc() function is used for allocating block of memory at
runtime. This function reserves a block of memory of given
size and returns a pointer of type void.
Ptr=(cast-type*) malloc (byte-size);
EXAMPLE PROGRAM
Output:
Enter the emp details
eno 1
ename priya
esal 10,000
#include <stdio.h>
#include <stdlib.h>
struct emp
{
int eno;
char name;
float esal;
void main()
{
struct emp *ptr;
ptr = (struct emp *) malloc(size of(structemp));
if(ptr == null)
{
pintf(“out of memory”);
}
else
{
printf(“Enter the emp deitals”);
scanf(“%d%s%f,&ptr-> eno,ptr-> name,&ptr-> esal”);
return 0;
}
ALLOCATION A BLOCK OF MEMORY:
CALLOC
calloc() is another memory allocation function that is used for
allocating memory at runtime. calloc function is normally used for
allocating memory to derived data types such as arrays and
structures.
Ptr=(cast-type*)calloc(n,elem-size);
EXAMPLE PROGRAM
output:
Number of elements to be entered :3
Enter 3 numbers:
22
55
14
The numbers entered are :22 55 14
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
int *a;
printf("Number of elements to be entered:");
scanf("%d",&n);
a = (int*)calloc(n, sizeof(int));
printf("Enter %d numbers:n",n);
for( i=0 ; i < n ; i++ )
{
scanf("%d",&a[i]);
}
printf("The numbers entered are: ");
for( i=0 ; i < n ; i++ )
{
printf("%d ",a[i]);
}
free( a );
return(0);
}
ALTERING THE SIZE OF A BLOCK :REALLOC
realloc() changes memory size that is already allocated
dynamically to a variable.
ptr=REALLOC(ptr,new size);
EXAMPLE PROGRAM
Output:
10 20 30
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int)*2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *)realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for(i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
return 0;
}
RELEASING THE USED SPACE: FREE
Free() function should be called on a pointer that was used either with
”calloc()” or “malloc()”,otherwise the function will destroy the memory
management making a system to crash.
free (ptr)
EXAMPLE:
#include<stdio.h>
int main()
{
int *str;
-------
-------
free(str);
}
References :
THANK YOU!

More Related Content

PPTX
Dynamic memory allocation
PPTX
Dynamic Memory Allocation(DMA)
PPT
Dynamic Memory Allocation
PPTX
Dynamic Memory allocation
PPTX
Exception handling c++
PPTX
Dynamic memory allocation in c
PPTX
Compiler vs interpreter
PPTX
Malloc() and calloc() in c
Dynamic memory allocation
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation
Dynamic Memory allocation
Exception handling c++
Dynamic memory allocation in c
Compiler vs interpreter
Malloc() and calloc() in c

What's hot (20)

PPTX
PPTX
Program execution
PPTX
Classes objects in java
PPTX
Functions in c language
PPTX
Interrupts
PPTX
Introduction of Memory Management
PPTX
Garbage collection
PPTX
Pointer to function 1
PPTX
Segmentation in operating systems
PPTX
Data types
PPTX
Functions in c
PPTX
Message passing ( in computer science)
PPTX
Pointers in c++
PPTX
Input Output Organization
PPTX
PPTX
memory hierarchy
PPTX
Functional units
PPT
Operating system services 9
PPTX
Register organization, stack
PPTX
Modules in Python Programming
Program execution
Classes objects in java
Functions in c language
Interrupts
Introduction of Memory Management
Garbage collection
Pointer to function 1
Segmentation in operating systems
Data types
Functions in c
Message passing ( in computer science)
Pointers in c++
Input Output Organization
memory hierarchy
Functional units
Operating system services 9
Register organization, stack
Modules in Python Programming
Ad

Similar to Dynamic memory allocation (20)

PPTX
dynamicmemoryallocation.pptx
PPTX
Memory management CP
PPTX
Dynamic memory allocation
PPSX
4 dynamic memory allocation
PDF
Data Structure - Dynamic Memory Allocation
PPTX
C dynamic ppt
PDF
Programming in C language specially allocation
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PPTX
16 dynamic-memory-allocation
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
PPTX
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
final GROUP 4.pptx
PPTX
Dynamic Memory Allocation in C
PPTX
Dynamic memory allocation
PPTX
Structures_and_Files[1] - Read-Only.pptx
DOCX
Dma
dynamicmemoryallocation.pptx
Memory management CP
Dynamic memory allocation
4 dynamic memory allocation
Data Structure - Dynamic Memory Allocation
C dynamic ppt
Programming in C language specially allocation
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
16 dynamic-memory-allocation
CLanguage_ClassPPT_3110003_unit 9Material.ppt
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
final GROUP 4.pptx
Dynamic Memory Allocation in C
Dynamic memory allocation
Structures_and_Files[1] - Read-Only.pptx
Dma
Ad

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation theory and applications.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Cloud computing and distributed systems.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation theory and applications.pdf
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Unlocking AI with Model Context Protocol (MCP)
Programs and apps: productivity, graphics, security and other tools
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Dynamic memory allocation

  • 1. Dynamic Memory Allocation Subject : C Language Name : Uttam Verma
  • 2. SYNOPSIS • Memory allocation • Static MemoryAllocation • Memory Allocation Process • Memory Allocation Functions • Allocation A Block Of Memory :Malloc • Allocation A Block Of Memory :Calloc • Altering The Size Of A Block :Realloc • Releasing The Used Space: Free
  • 4. MEMORYALLOCATION • The blocks of information in a memory system is called memory allocation. • To allocate memory it is necessary to keep in information of available memory in the system. If memory management system finds sufficient free memory, it allocates only as much memory as needed, keeping the rest available to satisfy future request. • Memory allocation has two types. They are static and dynamic memory allocation.
  • 5. STATIC MEMORY ALLOCATION • In static memory allocation, size of the memory may be required and that must be defined before loading and executing the program. DYNAMIC MEMORYALLOCATION • In the dynamic memory allocation, the memory is allocated to a variable or program at the run time. • The only way to access this dynamically allocated memory is through pointer.
  • 7. ALLOCATION A BLOCK OF MEMORY: MALLOC malloc() function is used for allocating block of memory at runtime. This function reserves a block of memory of given size and returns a pointer of type void. Ptr=(cast-type*) malloc (byte-size);
  • 8. EXAMPLE PROGRAM Output: Enter the emp details eno 1 ename priya esal 10,000 #include <stdio.h> #include <stdlib.h> struct emp { int eno; char name; float esal; void main() { struct emp *ptr; ptr = (struct emp *) malloc(size of(structemp)); if(ptr == null) { pintf(“out of memory”); } else { printf(“Enter the emp deitals”); scanf(“%d%s%f,&ptr-> eno,ptr-> name,&ptr-> esal”); return 0; }
  • 9. ALLOCATION A BLOCK OF MEMORY: CALLOC calloc() is another memory allocation function that is used for allocating memory at runtime. calloc function is normally used for allocating memory to derived data types such as arrays and structures. Ptr=(cast-type*)calloc(n,elem-size);
  • 10. EXAMPLE PROGRAM output: Number of elements to be entered :3 Enter 3 numbers: 22 55 14 The numbers entered are :22 55 14 #include <stdio.h> #include <stdlib.h> int main() { int i, n; int *a; printf("Number of elements to be entered:"); scanf("%d",&n); a = (int*)calloc(n, sizeof(int)); printf("Enter %d numbers:n",n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf("The numbers entered are: "); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } free( a ); return(0); }
  • 11. ALTERING THE SIZE OF A BLOCK :REALLOC realloc() changes memory size that is already allocated dynamically to a variable. ptr=REALLOC(ptr,new size);
  • 12. EXAMPLE PROGRAM Output: 10 20 30 #include <stdio.h> #include <stdlib.h> int main() { int *ptr = (int *)malloc(sizeof(int)*2); int i; int *ptr_new; *ptr = 10; *(ptr + 1) = 20; ptr_new = (int *)realloc(ptr, sizeof(int)*3); *(ptr_new + 2) = 30; for(i = 0; i < 3; i++) printf("%d ", *(ptr_new + i)); return 0; }
  • 13. RELEASING THE USED SPACE: FREE Free() function should be called on a pointer that was used either with ”calloc()” or “malloc()”,otherwise the function will destroy the memory management making a system to crash. free (ptr)