%in Benoni+277-882-255-28 abortion pills for sale in Benoni
Memory allocation in c
1.
2. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
3. Dynamic memory allocation in C
MUHAMMEDTHANVEER.M
kutubmadar@gmail.com
www.facebook.com/ thanveer
twitter.com/username
in.linkedin.com/in/profilename
9526960445
4. The process of allocating memory during
program execution is called dynamic memory
allocation.
4 dynamic memory allocation functions
S.No Function Syntax
1 malloc () malloc (number *sizeof(int));
2 calloc () calloc (number, sizeof(int));
3 realloc () realloc (pointer_name, number * sizeof(int));
4 free () free (pointer_name);
5. 1. malloc() function in C:
malloc () function is used to allocate space in memory during the execution
f the program.
malloc () does not initialize the memory allocated during execution. It
carries garbage value.
malloc () function returns null pointer if it couldn’t able to allocate
requested amount of memory.
7. 2. calloc() function in C:
calloc () function is also like malloc () function. But calloc ()
initializes the allocated memory to zero. But, malloc()
doesn’t.
9. 3. realloc() function in C:
realloc () function modifies the allocated memory size by malloc () and calloc () functions to new
size.
If enough space doesn’t exist in memory of current block to extend, new block is allocated for
the full size of reallocation, then copies the existing data to new block and then frees the old
block.
4. free() function in C:
free () function frees the allocated memory by malloc (), calloc (), realloc () functions
and returns the memory to the system.
10. EXAMPLE
//insert extra numbers
#include<stdio.h>//header
#include<stdlib.h>
int main()//main function
{
int int_n,int_i,int_j,int_m,*ptr_b;//local variable declaration
char str_a;
printf("enter number of element");//by user
scanf("%d",&int_n);//read and assign
ptr_b=(int*)calloc(int_n,sizeof(int));//malloc memory allocation
if(ptr_b==NULL)
{
printf("ERROR!memory not allocated");
exit(0);
}
printf("Now,enter the element");//by user
for(int_i=0;int_i<int_n;++int_i)//check condition
{
scanf("%d",ptr_b+int_i);//block of statement
}
11. printf("do you want to enter more numbers?.y/n");
scanf("%s",&str_a);
if(str_a=='y'||str_a=='Y')//check condition using if
{
printf("enter the number of extra element:");
scanf("%d",&int_m);
printf("enter the new elements:");
ptr_b=(int*)realloc(ptr_b,int_n+int_m);//create realloc memory
for(int_j=int_n;int_j<(int_n+int_m);int_j++)//check condition using loop
{
scanf("%d",ptr_b+int_j);
}
for(int_i=0;int_i<(int_n+int_m);++int_i)
{
printf("%d",*(ptr_b+int_i));//print the value
}
}
getch();
free(ptr_b);//clear the allocated memory
return 0;
}
12. OUT PUT
Enter number of element
3
Now,enter the element
4
5
6
Do you want to enter more numbers?.y/n
Y
Enter the number of extra elements
2
Enter the new element
8
9
45689
14. If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
15. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com