SlideShare a Scribd company logo
5
Most read
7
Most read
12
Most read
Memory Management
BY:- SHUBHAM SINHA
What is Memory Management
Memory management is a process of managing the
computer memory. In this available memory is
allocated to the variables in the program and frees
the memory when no longer needed by the
program.
Memory Management process is divided into
•Static memory allocation.
•Dynamic memory allocation.
Static memory allocation
In this type of allocation, memory space needed by
the variable in the program is allocated before
loading and executing the program. In this type of
allocation the allocated memory space will remain
unchanged as long as program is running.
Example :- int name[1000]
This declaration reserves 1000 memory locations in the
name NAME for use in the program.
Dynamic memory allocation
• C dynamic memory allocation refers to performing manual memory
management for dynamic memory allocation in the C programming
language via a group of functions in the C standard library, namely
malloc, realloc, calloc and free.
• 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.
Dynamic memory allocation can be implemented in
c with the help of the library function.
•Malloc()
•Calloc()
•Realloc()
•Free()
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;
char name; float esal;
void main()
{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;
}
Output:
Enter the emp details
eno 1
ename SHUBHAM
esal 10,000
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()
{
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);
}
output:
Number of elements to be entered:3
Enter 3 numbers:
22
55
14
The numbers entered are :22 55 14
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()
{
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;
}
Output:
10 20 30
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.
EXAMPLE:
func()
{
int *ptr, *p;
ptr = new int[100];
p = new int;
delete[] ptr;
delete p;
}
free (ptr)
Memory management  CP

More Related Content

PPTX
Memory hierarchy
Srirengasrirenga
 
PDF
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
PPTX
Member Function in C++
NikitaKaur10
 
PPT
Memory allocation in c
Prabhu Govind
 
PPTX
Dynamic Memory Allocation(DMA)
Kamal Acharya
 
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
DOC
C lab-programs
Tony Kurishingal
 
PPSX
Break and continue
Frijo Francis
 
Memory hierarchy
Srirengasrirenga
 
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
Member Function in C++
NikitaKaur10
 
Memory allocation in c
Prabhu Govind
 
Dynamic Memory Allocation(DMA)
Kamal Acharya
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
C lab-programs
Tony Kurishingal
 
Break and continue
Frijo Francis
 

What's hot (20)

PPTX
Union in c language
tanmaymodi4
 
PDF
Function in C
Dr. Abhineet Anand
 
PDF
Lecture19 unionsin c.ppt
eShikshak
 
PPTX
diffrence between procedure oriented programming & object oriented programmin...
nihar joshi
 
PPTX
File in C language
Manash Kumar Mondal
 
PPTX
C Structures and Unions
Dhrumil Patel
 
PPTX
memory hierarchy
sreelakshmikv
 
PPT
Strings Functions in C Programming
DevoAjit Gupta
 
PPTX
Assembly Language and microprocessor
Khaled Sany
 
PPTX
Computer architecture virtual memory
Mazin Alwaaly
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Handling of character strings C programming
Appili Vamsi Krishna
 
PPTX
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
PPTX
C function
thirumalaikumar3
 
PPTX
Dynamic memory allocation
Viji B
 
PPTX
String Handling in c++
Fahim Adil
 
PPTX
Typedef
vaseemkhn
 
PPT
Operation on string presentation
Aliul Kadir Akib
 
PPT
Enumerated data types in C
Arpana shree
 
PPTX
Dynamic memory allocation
UTTAM VERMA
 
Union in c language
tanmaymodi4
 
Function in C
Dr. Abhineet Anand
 
Lecture19 unionsin c.ppt
eShikshak
 
diffrence between procedure oriented programming & object oriented programmin...
nihar joshi
 
File in C language
Manash Kumar Mondal
 
C Structures and Unions
Dhrumil Patel
 
memory hierarchy
sreelakshmikv
 
Strings Functions in C Programming
DevoAjit Gupta
 
Assembly Language and microprocessor
Khaled Sany
 
Computer architecture virtual memory
Mazin Alwaaly
 
Functions in c language
tanmaymodi4
 
Handling of character strings C programming
Appili Vamsi Krishna
 
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
C function
thirumalaikumar3
 
Dynamic memory allocation
Viji B
 
String Handling in c++
Fahim Adil
 
Typedef
vaseemkhn
 
Operation on string presentation
Aliul Kadir Akib
 
Enumerated data types in C
Arpana shree
 
Dynamic memory allocation
UTTAM VERMA
 
Ad

Similar to Memory management CP (20)

PPTX
dynamicmemoryallocation.pptx
Niharika606186
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
PPTX
Dynamic Memory Allocation in C programming
Rhishav Poudyal
 
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
PPTX
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
DOCX
Dma
Acad
 
PPTX
Dynamic memory allocation
Gaurav Mandal
 
PPTX
final GROUP 4.pptx
ngonidzashemutsipa
 
PPTX
Dma
Joy Forerver
 
PPTX
Dynamic Memory Allocation in C Programming | IIES Guide
ibrahimseo2025
 
PPTX
C dynamic ppt
RJ Mehul Gadhiya
 
PPTX
Dynamic Memory allocation
Grishma Rajput
 
PDF
Dynamic memory allocation
Gem WeBlog
 
PPTX
Dynamic memory allocation
Mohammad Usman
 
PPTX
Structures_and_Files[1] - Read-Only.pptx
Ramesh Bssv
 
PPTX
dynamic_v1-3.pptx
ngonidzashemutsipa
 
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
PPT
Presentation on dynamic memory management
Bhimsen Joshi
 
dynamicmemoryallocation.pptx
Niharika606186
 
Dynamic memory allocation in c
lavanya marichamy
 
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
Dynamic Memory Allocation in C programming
Rhishav Poudyal
 
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
Dma
Acad
 
Dynamic memory allocation
Gaurav Mandal
 
final GROUP 4.pptx
ngonidzashemutsipa
 
Dynamic Memory Allocation in C Programming | IIES Guide
ibrahimseo2025
 
C dynamic ppt
RJ Mehul Gadhiya
 
Dynamic Memory allocation
Grishma Rajput
 
Dynamic memory allocation
Gem WeBlog
 
Dynamic memory allocation
Mohammad Usman
 
Structures_and_Files[1] - Read-Only.pptx
Ramesh Bssv
 
dynamic_v1-3.pptx
ngonidzashemutsipa
 
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
Presentation on dynamic memory management
Bhimsen Joshi
 
Ad

Recently uploaded (20)

PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Inventory management chapter in automation and robotics.
atisht0104
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Zero Carbon Building Performance standard
BassemOsman1
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 

Memory management CP

  • 2. What is Memory Management Memory management is a process of managing the computer memory. In this available memory is allocated to the variables in the program and frees the memory when no longer needed by the program.
  • 3. Memory Management process is divided into •Static memory allocation. •Dynamic memory allocation.
  • 4. Static memory allocation In this type of allocation, memory space needed by the variable in the program is allocated before loading and executing the program. In this type of allocation the allocated memory space will remain unchanged as long as program is running. Example :- int name[1000] This declaration reserves 1000 memory locations in the name NAME for use in the program.
  • 5. Dynamic memory allocation • C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free. • 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.
  • 6. Dynamic memory allocation can be implemented in c with the help of the library function. •Malloc() •Calloc() •Realloc() •Free()
  • 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; char name; float esal; void main() {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; } Output: Enter the emp details eno 1 ename SHUBHAM esal 10,000
  • 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() { 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); } output: Number of elements to be entered:3 Enter 3 numbers: 22 55 14 The numbers entered are :22 55 14
  • 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() { 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; } Output: 10 20 30
  • 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. EXAMPLE: func() { int *ptr, *p; ptr = new int[100]; p = new int; delete[] ptr; delete p; } free (ptr)