0% found this document useful (0 votes)
2 views

Dynamic Memory Allocation by SA

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Dynamic Memory Allocation by SA

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Dynamic Memory Allocation

Presented by:
Shambhavi Agrawal
CS-2
12/23/2024 1
Table of Contents

• Memory Allocation
• Types of Memory Allocation
– Static Memory Allocation
– Dynamic Memory Allocation
• calloc() function
• malloc() function
• realloc() function
• free() function
• References

12/23/2024 2
Memory Allocation
• Memory allocation is a process by which computer
programs and services are assigned with physical or
virtual memory space
• Memory allocation is achieved through a process known
as memory management
• 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 as much memory as needed

12/23/2024 3
Types of memory allocation

• The program is allocated with


1.Static Memory memory at compile time
Allocation • Used in array

• The program is allocated with


2.Dynamic Memory memory at run time
Allocation • Used in linked list

12/23/2024 4
Dynamic Memory Allocation
• Whenever we define any array in a program, the fixed
block of memory is assigned to that array. This happens
through static memory allocation process. It creates a
problem when we do not have fixed number of elements
to be stored
• Pointer can be used to with array to resolve this problem
and to provide facility of dynamic array.
• Dynamic memory allocation is the process of providing
memory blocks to the variables at execution time of
program

12/23/2024 5
• To perform different memory allocation ,different
memory allocation method such as malloc(), calloc(),
realloc(),free()

• Allocates single block of requested


malloc() memory
• Allocates multiple block of
calloc() requested memory
•Reallocates the memory occupied by
realloc()
malloc() or calloc() functions
• Frees the dynamically allocated
free() memory
12/23/2024 6
malloc() function

• malloc() function allocates the single block of requested


memory
• It takes only one argument that specify the memory
block
• It does not initializes the allocated memory and contain
garbage value
• Syntax of malloc() function is given below:
ptr =( int*)malloc(byte-size)

12/23/2024 7
Example
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
Output
}
printf("Enter elements of array: "); Enter elements of elements: 3
for(i=0;i<n;++i) Enter elements of array: 10 10 10
{ Sum=30
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
12/23/2024 8
}
calloc() function

• The calloc() function allocates multiple block of


requested memory
• It takes two arguments that specify the number of
elements to be reserved and the size of each element in
bytes
• It initializes the allocated memory to zero
• Syntax:
ptr=(cast-type*)calloc(n , byte-size)
where n=number of blocks

12/23/2024 9
realloc() function

• If memory is not sufficient for malloc() or calloc(), then


you can reallocate the memory by realloc() function.
• In short, it changes the memory size.
• Syntax:
ptr=realloc( ptr, number*(size of( data type ));

12/23/2024 10
free() function

• The memory occupied by malloc() or calloc() functions


must be released by calling free() function. Otherwise, it
will consume memory until program exit.
• Syntax:
free(ptr)

12/23/2024 11
References

• https://www.javatpoint.com/dynamic-memory-allocation-i
n-c
• https://www.geeksforgeeks.org/dynamic-memory-allocatio
n-in-c-using-malloc-calloc-free-and-realloc/
• https://www.geeksforgeeks.org/what-is-dynamic-memory

12/23/2024 12
Thank You

12/23/2024 13

You might also like