Tutorial references

c
c++
c#

Tutorial references

chapters

malloc, calloc , realloc and free

Dynamic Memory Allocation

"stdlib.h" header file include in your program.

malloc()

malloc stands for "memory allocation".

It allocate space in memory at run time according to given size.

It allocate space in single block.

Syntax:

ptr = (cast-type*) malloc(byte-size);

In above syntax ptr is a pointer type variable.

If malloc() allocate space then it return pointer otherwise NULL.

free()

It is used to release run time allocated memory. It means free() can frees occupied memory of malloc() or calloc().

free(ptr);

Example

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main(){
int n,i,*ptr,sum=0;
clrscr();
printf("\nEnter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("\nSorry! Number of element has not given by you.");
exit(0);
}
for(i=0;i< n;i++)
{
printf("\nEnter elements of array: ");
scanf("%d",ptr+i);
}
for(i=0;i< n;i++)
{ printf("\n given element: %d",*(ptr+i));
sum = sum + *(ptr+i);
}
printf("\n Sum=%d",sum);
free(ptr);
getch();
}

Run program

calloc()

calloc stands for "contiguous allocation".

calloc() allocates multiple block of memory.

Difference between malloc() and calloc():- malloc() allocates single block of memory but calloc() allocates multiple block of memory.

ptr = (cast-type*)calloc(n, element-size);

Example

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main(){
int n,i,*ptr,sum=0;
clrscr();
printf("\nEnter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n, sizeof(int));
if(ptr==NULL)
{
printf("\nSorry! Number of element has not given by you.");
exit(0);
}
for(i=0;i< n;i++)
{
printf("\nEnter elements of array: ");
scanf("%d",ptr+i);
}
for(i=0;i< n;i++)
{ printf("\n given element: %d",*(ptr+i));
sum = sum + *(ptr+i);
}
printf("\n Sum=%d",sum);
free(ptr);
getch();
}

Run program

realloc()

If calloc() or malloc() allocated memory is not sufficient for requirement then we can change size of memory by realloc() at run time.

ptr =(cast-type*)realloc(ptr, newsize);

Here, ptr is reallocated with size of newsize.

Example

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main(){
int n1,n2,i,*ptr,sum=0,newSum=0;
clrscr();
printf("\nEnter size of array: ");
scanf("%d",&n1);
ptr=(int*)malloc(n1 * sizeof(int));
if(ptr==NULL){
printf("\nSorry! Number of element has not given by you.");
exit(0);
}
for(i=0;i< n1;i++){
printf("\nEnter elements of array: ");
scanf("%d",ptr+i);
}
for(i=0;i< n1;i++){
printf("\n given element: %d",*(ptr+i));
sum = sum + *(ptr+i);
}
printf("\n Sum=%d",sum);
printf("\nEnter new size of array: ");
scanf("%d",&n2);
ptr = (int *)realloc(ptr, n2);
for(i=0;i< n2;i++){
printf("\nEnter new elements of array: ");
scanf("%d",ptr+i);
}
for(i=0;i< n2;i++){
printf("\n given new element: %d",*(ptr+i));
newSum = newSum + *(ptr+i);
}
printf("\n New Sum=%d",newSum);
free(ptr);
getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018