malloc() and calloc() in C
Use of malloc():
 It allocates a block of size bytes from memory heap.
 It allows a program to allocate memory as its ne...
Use of calloc():
 It provides access to the C memory heap, which is available for dynamic
allocation variable-sized block...
Illustrative programs:
/*Program 1: illustration of malloc()*/
#include<stdio.h>

malloc() comes under it

#include<conio....
Illustrative programs(contd.):
/*program 1 contd.*/
printf(“%d”,ptr);
free(ptr);

Allocated memory is freed

getch();

}
I...
Illustrative programs(contd.):
Output of program 1 is:
Ilustrative programs(contd.):
/*Program 2: Illustration of calloc()*/
#include<stdio.h>

calloc() comes under it

#include...
Illustrative programs(contd.):
/*program 2 contd.*/
for(i=0;i<7;i++)
{
printf(“n%d”,*ptr+a[i]);

}
free(ptr);
getch();
}

...
Illustrative programs(contd.):
In program 2 6 blocks of memory is allocated using calloc() with each block
having 2 bytes ...
Upcoming SlideShare
Loading in …5
×

Malloc() and calloc() in c

875 views

Published on

This is another ppt on malloc and call

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
875
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
43
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Malloc() and calloc() in c

  1. 1. malloc() and calloc() in C
  2. 2. Use of malloc():  It allocates a block of size bytes from memory heap.  It allows a program to allocate memory as its needed, and in the exact amount needed.
  3. 3. Use of calloc():  It provides access to the C memory heap, which is available for dynamic allocation variable-sized block of memory.
  4. 4. Illustrative programs: /*Program 1: illustration of malloc()*/ #include<stdio.h> malloc() comes under it #include<conio.h> #include<stdlib.h> void main() { int a,*ptr; a=10; ptr=(int*)malloc(a*sizeof(int)); ptr=a; Allocating memory to ptr
  5. 5. Illustrative programs(contd.): /*program 1 contd.*/ printf(“%d”,ptr); free(ptr); Allocated memory is freed getch(); } In this program the sizeof(int) is the size given thorugh malloc() to ptr. Now ptr is assigned the value of a. ptr=a; so the value 10 is assigned to ptr, for which, we dynamically allocated memory space using malloc.
  6. 6. Illustrative programs(contd.): Output of program 1 is:
  7. 7. Ilustrative programs(contd.): /*Program 2: Illustration of calloc()*/ #include<stdio.h> calloc() comes under it #include<conio.h> #include<stdlib.h> void main() { int *ptr,a[6]={1,2,3,4,5,6}; int i; ptr=(int*)calloc(a[6]*sizeof(int),2); Allocating memory for ptr
  8. 8. Illustrative programs(contd.): /*program 2 contd.*/ for(i=0;i<7;i++) { printf(“n%d”,*ptr+a[i]); } free(ptr); getch(); } Allocated memory is freed
  9. 9. Illustrative programs(contd.): In program 2 6 blocks of memory is allocated using calloc() with each block having 2 bytes of space. Now variable i is used in for to cycle the loop 6 times on incremental mode. On each cycle the data in allocated memory in ptr is printed using *ptr+a[i]. Output of program 2:

×