SlideShare a Scribd company logo
Dynamic Memory Allocation
Basic Idea
• Many a time we face situations where data is dynamic in nature.
• – Amount of data cannot be predicted beforehand.
• – Number of data items keeps changing during program execution.
• Such situations can be handled more easily and effectively using
dynamic memory management techniques.
• C language requires the number of elements in an array to be
specified at compile time.
• – Often leads to wastage of memory space or program failure.
• Dynamic Memory Allocation
• – Memory space required can be specified at the time of execution.
• – C supports allocating and freeing memory dynamically using library
routines.
Memory Allocation Process in C
• Memory allocated to any program is either from Stack or Heap.
• Stack memory is allocated to local variable, function call parameters etc.
• Heap memory is allocated to global variables or the variables which request
for memory space and asking for memory space is called dynamic allocation
of memory.
• Four basic functions which belong to stdlib.h or malloc.h or alloc.h for
dynamic memory allocation –
1. malloc()
2. calloc()
3. realloc()
4. free()
16 dynamic-memory-allocation
16 dynamic-memory-allocation
16 dynamic-memory-allocation
16 dynamic-memory-allocation
16 dynamic-memory-allocation
Example
void main(){
int i,N;
float *height;
float sum=0,avg;
printf("Input no. of studentsn");
scanf("%d", &N);
height = (float *) malloc(N * sizeof(float));
printf("Input heights for %d students n",N);
for (i=0; i<N; i++)
scanf ("%f", &height[i]);
for(i=0;i<N;i++)
sum += height[i];
avg = sum / (float) N;
printf("Average height = %f n“,avg);
free (height);
}
16 dynamic-memory-allocation
Calloc()
• Calloc function allocates the block of bytes from
heap.
• Syntax: void * calloc (num, size)
• This function takes two arguments that specify
the number of elements to be reserved, and the
size of each element in bytes
• It allocates memory block equivalent to num*
size .
Example2: calloc and malloc
16 dynamic-memory-allocation
16 dynamic-memory-allocation
Realloc Example
void main() {
int* array,*array1;
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
array = (int*) calloc(n,sizeof(int));
for (i=0; i<n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &array[i]);
}
array1 = (int*) realloc(array,(n+1)*sizeof(int));
if(array1)
printf("Memory Allocated");
printf("nThe Dynamic Array is: n");
for (i=0; i<(n+1); i++)
printf("The value of %d is %dn", i, array1[i]);
}
O/P?
#include <malloc.h>
#include <stdio.h>
void main() {
int* ptr;
ptr=(int *)malloc(sizeof(int));
*ptr=5;
printf("%d ",*ptr);
free(ptr);
*ptr=7;
printf("%d ",*ptr);
}

More Related Content

PDF
Constructors and Destructors
PPTX
Dynamic memory allocation
PPTX
Dynamic memory allocation in c++
PPTX
classes and objects in C++
PPTX
Polymorphism In c++
PPTX
Dynamic memory Allocation in c language
PPTX
Functions in c
PPTX
Dynamic memory allocation in c
Constructors and Destructors
Dynamic memory allocation
Dynamic memory allocation in c++
classes and objects in C++
Polymorphism In c++
Dynamic memory Allocation in c language
Functions in c
Dynamic memory allocation in c

What's hot (20)

PPT
16717 functions in C++
 
PPTX
Exception handling c++
PPTX
Static Members-Java.pptx
PPTX
Pointers in c++
PPT
Memory allocation in c
PPTX
Dynamic memory allocation
PPTX
Pointer in c
PPTX
Dynamic Memory Allocation(DMA)
PPTX
PDF
Constructor and Destructor
PPTX
Data members and member functions
PPTX
Union in C programming
PPTX
Member Function in C++
PPTX
Constructor in java
PPTX
Dynamic Memory allocation
PPTX
Stacks IN DATA STRUCTURES
PPTX
array of object pointer in c++
PPTX
Types of Constructor in C++
PPT
Function overloading(c++)
PPTX
Type conversion
16717 functions in C++
 
Exception handling c++
Static Members-Java.pptx
Pointers in c++
Memory allocation in c
Dynamic memory allocation
Pointer in c
Dynamic Memory Allocation(DMA)
Constructor and Destructor
Data members and member functions
Union in C programming
Member Function in C++
Constructor in java
Dynamic Memory allocation
Stacks IN DATA STRUCTURES
array of object pointer in c++
Types of Constructor in C++
Function overloading(c++)
Type conversion
Ad

Similar to 16 dynamic-memory-allocation (20)

PPTX
Memory management CP
PPTX
Dynamic memory allocation
PPTX
dynamicmemoryallocation.pptx
PPT
Dynamic Memory Allocation
PPTX
C dynamic ppt
PPTX
PDF
dynamic-allocation.pdf
PPTX
DIY Java Profiling
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PPTX
Introduction to Data Structures, Data Structures using C.pptx
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
PPTX
Presentation on Heap Sort
PPTX
C language
PDF
Programming in C language specially allocation
PPTX
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
PPTX
Dynamic Memory Allocation in c prog.pptx
PPTX
Programming Language
PPTX
C programming language tutorial
Memory management CP
Dynamic memory allocation
dynamicmemoryallocation.pptx
Dynamic Memory Allocation
C dynamic ppt
dynamic-allocation.pdf
DIY Java Profiling
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
Introduction to Data Structures, Data Structures using C.pptx
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
Presentation on Heap Sort
C language
Programming in C language specially allocation
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Dynamic Memory Allocation in c prog.pptx
Programming Language
C programming language tutorial
Ad

More from Rohit Shrivastava (13)

PPT
1 introduction-to-computer
PPTX
17 structure-and-union
PPT
14 strings
PPT
11 functions
PPT
PPT
8 number-system
PPT
7 decision-control
PPT
6 operators-in-c
PPT
5 introduction-to-c
PPT
3 algorithm-and-flowchart
PPT
2 memory-and-io-devices
PPT
4 evolution-of-programming-languages
PPT
6 operators-in-c
1 introduction-to-computer
17 structure-and-union
14 strings
11 functions
8 number-system
7 decision-control
6 operators-in-c
5 introduction-to-c
3 algorithm-and-flowchart
2 memory-and-io-devices
4 evolution-of-programming-languages
6 operators-in-c

Recently uploaded (20)

PDF
Volvo EC290C NL EC290CNL excavator weight.pdf
PDF
Caterpillar CAT 311B EXCAVATOR (8GR00001-UP) Operation and Maintenance Manual...
PPTX
vsdfhlahsadfjkhasihdflakjsdfhlajdhlfkjahfdljkash
PDF
computer system to create, modify, analyse or optimize an engineering design.
PPTX
Robot_ppt_YRG[1] [Read-Only]bestppt.pptx
PPTX
Intro to ISO 9001 2015.pptx for awareness
PPTX
Type of Sentence & SaaaaaaaaaadddVA.pptx
PDF
Volvo ecr58 plus Service Manual Download
PPT
ACCOMPLISHMENT REPOERTS AND FILE OF GRADE 12 2021.ppt
PDF
Diagnose and Repair Transmission Sound Issues in Volkswagen Vehicles
PDF
Volvo ecr88 lifting capacity Service Repair Manual.pdf
PDF
Renesas R-Car_Cockpit_overview210214-Gen4.pdf
PDF
3-REasdfghjkl;[poiunvnvncncn-Process.pdf
PDF
Volvo ecr58 problems Repair Manual Pdf Download
PDF
Honda Dealership SNS Evaluation pdf/ppts
PDF
EC300D LR EC300DLR - Volvo Service Repair Manual.pdf
PDF
120725175041.pdfhjjjjjjjjjjjjjjjjjjjjjjh
PDF
Physics class 12thstep down transformer project.pdf
PPTX
1. introduction-to-bvcjdhjdfffffffffffffffffffffffffffffffffffmicroprocessors...
PPTX
Gayatri Cultural Educational Society.pptx
Volvo EC290C NL EC290CNL excavator weight.pdf
Caterpillar CAT 311B EXCAVATOR (8GR00001-UP) Operation and Maintenance Manual...
vsdfhlahsadfjkhasihdflakjsdfhlajdhlfkjahfdljkash
computer system to create, modify, analyse or optimize an engineering design.
Robot_ppt_YRG[1] [Read-Only]bestppt.pptx
Intro to ISO 9001 2015.pptx for awareness
Type of Sentence & SaaaaaaaaaadddVA.pptx
Volvo ecr58 plus Service Manual Download
ACCOMPLISHMENT REPOERTS AND FILE OF GRADE 12 2021.ppt
Diagnose and Repair Transmission Sound Issues in Volkswagen Vehicles
Volvo ecr88 lifting capacity Service Repair Manual.pdf
Renesas R-Car_Cockpit_overview210214-Gen4.pdf
3-REasdfghjkl;[poiunvnvncncn-Process.pdf
Volvo ecr58 problems Repair Manual Pdf Download
Honda Dealership SNS Evaluation pdf/ppts
EC300D LR EC300DLR - Volvo Service Repair Manual.pdf
120725175041.pdfhjjjjjjjjjjjjjjjjjjjjjjh
Physics class 12thstep down transformer project.pdf
1. introduction-to-bvcjdhjdfffffffffffffffffffffffffffffffffffmicroprocessors...
Gayatri Cultural Educational Society.pptx

16 dynamic-memory-allocation

  • 2. Basic Idea • Many a time we face situations where data is dynamic in nature. • – Amount of data cannot be predicted beforehand. • – Number of data items keeps changing during program execution. • Such situations can be handled more easily and effectively using dynamic memory management techniques. • C language requires the number of elements in an array to be specified at compile time. • – Often leads to wastage of memory space or program failure. • Dynamic Memory Allocation • – Memory space required can be specified at the time of execution. • – C supports allocating and freeing memory dynamically using library routines.
  • 3. Memory Allocation Process in C • Memory allocated to any program is either from Stack or Heap. • Stack memory is allocated to local variable, function call parameters etc. • Heap memory is allocated to global variables or the variables which request for memory space and asking for memory space is called dynamic allocation of memory. • Four basic functions which belong to stdlib.h or malloc.h or alloc.h for dynamic memory allocation – 1. malloc() 2. calloc() 3. realloc() 4. free()
  • 9. Example void main(){ int i,N; float *height; float sum=0,avg; printf("Input no. of studentsn"); scanf("%d", &N); height = (float *) malloc(N * sizeof(float)); printf("Input heights for %d students n",N); for (i=0; i<N; i++) scanf ("%f", &height[i]); for(i=0;i<N;i++) sum += height[i]; avg = sum / (float) N; printf("Average height = %f n“,avg); free (height); }
  • 11. Calloc() • Calloc function allocates the block of bytes from heap. • Syntax: void * calloc (num, size) • This function takes two arguments that specify the number of elements to be reserved, and the size of each element in bytes • It allocates memory block equivalent to num* size .
  • 15. Realloc Example void main() { int* array,*array1; int n, i; printf("Enter the number of elements: "); scanf("%d", &n); array = (int*) calloc(n,sizeof(int)); for (i=0; i<n; i++) { printf("Enter number %d: ", i); scanf("%d", &array[i]); } array1 = (int*) realloc(array,(n+1)*sizeof(int)); if(array1) printf("Memory Allocated"); printf("nThe Dynamic Array is: n"); for (i=0; i<(n+1); i++) printf("The value of %d is %dn", i, array1[i]); }
  • 16. O/P? #include <malloc.h> #include <stdio.h> void main() { int* ptr; ptr=(int *)malloc(sizeof(int)); *ptr=5; printf("%d ",*ptr); free(ptr); *ptr=7; printf("%d ",*ptr); }