SlideShare a Scribd company logo
4
Most read
7
Most read
8
Most read
DYNAMIC MEMORY ALLOCATION
IN C
PRESENTED BY
M.LAVANYA
M.Sc(CS&IT)
NSCAS
SYNOPSIS
• Memory allocation
• Static Memory Allocation
• 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
MEMORY ALLOCATION
• 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.
• In 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 for
the that must be define before loading and executing the program.
DYNAMIC MEMORY ALLOCATION
• 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 PROCESS
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
#include <stdio.h>
#include <stdlib.h>
struct emp
{
int eno; Output:
char name; Enter the emp details
float esal; eno 1
void main() ename priya
{ esal 10,000
struct emp *ptr;
ptr = (struct emp *) malloc(size of(struct emp));
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
#include <stdio.h>
#include <stdlib.h>
int main()
{ output:
int i, n;
int *a; Number of elements to be entered :3
printf("Number of elements to be entered:"); Enter 3 numbers:
scanf("%d",&n); 22
a = (int*)calloc(n, sizeof(int)); 55
printf("Enter %d numbers:n",n); 14
for( i=0 ; i < n ; i++ ) The numbers entered are :22 55 14
{
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
#include <stdio.h>
#include <stdlib.h>
int main()
{ Output:
int *ptr = (int *)malloc(sizeof(int)*2);
int i; 10 20 30
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:
func()
{
int *ptr, *p;
ptr = new int[100];
p = new int;
delete[] ptr;
delete p;
}
Dynamic memory allocation in c

More Related Content

PPTX
Dynamic memory allocation
PPTX
Introduction to data structure ppt
PDF
Data structure ppt
PPTX
Pointer in c
PPTX
Functions in C
PPTX
Data types in C
PDF
Learning Robotic Process Automation-1-80
Dynamic memory allocation
Introduction to data structure ppt
Data structure ppt
Pointer in c
Functions in C
Data types in C
Learning Robotic Process Automation-1-80

What's hot (20)

PPT
Variables in C Programming
PPTX
Programming in c Arrays
PPTX
Strings in C
PPTX
Function in C program
PPTX
Pointers in C Programming
PPTX
data types in C programming
PPTX
classes and objects in C++
PPTX
Python Functions
PPTX
Array ppt
PDF
Constructors and Destructors
PPT
Array in c
PPT
Basics of C programming
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
arrays and pointers
PPTX
Presentation on Function in C Programming
PPTX
INLINE FUNCTION IN C++
PPT
structure and union
PPTX
Structure in C
PPTX
Inline function
PPT
Control structure C++
Variables in C Programming
Programming in c Arrays
Strings in C
Function in C program
Pointers in C Programming
data types in C programming
classes and objects in C++
Python Functions
Array ppt
Constructors and Destructors
Array in c
Basics of C programming
Array Introduction One-dimensional array Multidimensional array
arrays and pointers
Presentation on Function in C Programming
INLINE FUNCTION IN C++
structure and union
Structure in C
Inline function
Control structure C++
Ad

Similar to Dynamic memory allocation in c (20)

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

More from lavanya marichamy (16)

PPTX
Digital video
PPTX
Network design consideration
PPTX
Java servlets and CGI
PPTX
Data structure - traveling sales person and mesh algorithm
PPTX
Fundamentals and image compression models
PPTX
Software requirements specification
PPTX
Data mining primitives
PPTX
Memory Management
PPTX
Query evaluation and optimization
PPTX
Basic Computer Organisation And Design
PPTX
Register Transfer Language,Bus and Memory Transfer
PPTX
Arithmetic micro operations
PPTX
Recovery with concurrent transaction
PPTX
microcomputer architecture-Instruction formats
PPTX
IEEE STANDARED 802.5 LAN
PPTX
Broadband isdn and atm
Digital video
Network design consideration
Java servlets and CGI
Data structure - traveling sales person and mesh algorithm
Fundamentals and image compression models
Software requirements specification
Data mining primitives
Memory Management
Query evaluation and optimization
Basic Computer Organisation And Design
Register Transfer Language,Bus and Memory Transfer
Arithmetic micro operations
Recovery with concurrent transaction
microcomputer architecture-Instruction formats
IEEE STANDARED 802.5 LAN
Broadband isdn and atm

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Onica Farming 24rsclub profitable farm business
PDF
English Language Teaching from Post-.pdf
PDF
Types of Literary Text: Poetry and Prose
PPTX
IMMUNIZATION PROGRAMME pptx
PDF
From loneliness to social connection charting
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
How to Manage Bill Control Policy in Odoo 18
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
UPPER GASTRO INTESTINAL DISORDER.docx
102 student loan defaulters named and shamed – Is someone you know on the list?
Cardiovascular Pharmacology for pharmacy students.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Onica Farming 24rsclub profitable farm business
English Language Teaching from Post-.pdf
Types of Literary Text: Poetry and Prose
IMMUNIZATION PROGRAMME pptx
From loneliness to social connection charting
How to Manage Loyalty Points in Odoo 18 Sales
The Final Stretch: How to Release a Game and Not Die in the Process.
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
How to Manage Bill Control Policy in Odoo 18
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf

Dynamic memory allocation in c

  • 1. DYNAMIC MEMORY ALLOCATION IN C PRESENTED BY M.LAVANYA M.Sc(CS&IT) NSCAS
  • 2. SYNOPSIS • Memory allocation • Static Memory Allocation • 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
  • 3. MEMORY ALLOCATION • 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. • In memory allocation has two types. They are static and dynamic memory allocation.
  • 4. STATIC MEMORY ALLOCATION • In static memory allocation, size of the memory may be required for the that must be define before loading and executing the program. DYNAMIC MEMORY ALLOCATION • 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 #include <stdio.h> #include <stdlib.h> struct emp { int eno; Output: char name; Enter the emp details float esal; eno 1 void main() ename priya { esal 10,000 struct emp *ptr; ptr = (struct emp *) malloc(size of(struct emp)); 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 #include <stdio.h> #include <stdlib.h> int main() { output: int i, n; int *a; Number of elements to be entered :3 printf("Number of elements to be entered:"); Enter 3 numbers: scanf("%d",&n); 22 a = (int*)calloc(n, sizeof(int)); 55 printf("Enter %d numbers:n",n); 14 for( i=0 ; i < n ; i++ ) The numbers entered are :22 55 14 { 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 #include <stdio.h> #include <stdlib.h> int main() { Output: int *ptr = (int *)malloc(sizeof(int)*2); int i; 10 20 30 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) EXAMPLE: func() { int *ptr, *p; ptr = new int[100]; p = new int; delete[] ptr; delete p; }