Search Bar

Header Ads

Dynamic memory allocation in c language

Dynamic memory allocation and releasing dynamically allocated memory.



Dynamic memory allocation and releasing dynamically allocated memory.
Dynamic memory allocation and releasing dynamically allocated memory.

Hello friends in this post I am going to teach you what is Dynamic memory allocation and releasing dynamically allocated memory and also some program to that function.

And also teach you why Dynamic memory allocation and releasing dynamically allocated memory is use and how can you implement this functions for memory allocation in your program.

So let's learn what is mean by Dynamic memory allocation and releasing dynamically allocated memory

The method of allocation memory during programs execution is called Dynamic memory.

C programing offers 4 dynamic memory allocation functions. They are,

1.malloc()
2.calloc()
3.realloc()
4.free()

1.malloc()
The function is used to allocate memory during the execution of the program. It does not initialize the memory allocated during execution. It carries garbage value. It return null pointer if it couldn't able to allocate requested amount of memory.

Malloc () in c language
Malloc() function in c 


So for example see this program and try it to run.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char name[20];
char *address;
strcpy(name,"example");
address=(char*)malloc(50 * sizeof(char));
strcpy(address, "demo program");
printf("example=%s\n,name);
printf("example: %s\n,address);
return 0;
}
So after run this program output is
Example=example
Example: demo program

In above example, we assigned and printed the name for address, we estimated that the number of characters should be around 50. So, the size of address will be 50 *sizeof (char).

2.CALLOC()
This function is same like malloc() function but difference is calloc() function initialize the allocated memory to zero. But malloc() doesn't.


Calloc () in c language
Calloc () in c language

Note:- You Can See Program For Calloc() function On Internet..

3.REALLOC() FUNCTION
This function modified the allocated memory size by functions malloc() and calloc() to new size.




Realloc () in c language

If enough memory doesn't exist in memory of current block to extend, new block is allocated for the full size of relocation, then copies the existing data to new block and then frees the old block

Example program

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char *p1;
int m1,m2;
m1=10;
m2=30;
p1=(char*)malloc(m1);
strcpy(p1," reallocation practice ");
p1=(char*)realloc(p1,m2);
strcat(p1," example ");
printf(" %s\n",p1);
return 0;
}

Output
Reallocation practice.

4. FREE () Function:-
This function Free the allocated memory by functions malloc (),calloc(),realloc() and return the memory to the system.





Example program

#include<stdio.h>
#include<stblib.h>
int main ()
{
int n,I,*p;
printf("\n Enter Number Of Elements:");
scanf("%d",&n);
p=(int*)malloc(n * sizeof(into));
if(p==NULL)
{
printf ("\n Memory cannot be allocated ");
}
else
{
printf(" \n Enter elements of array ");
for(I=0;I<n;i++)
{
scanf(" %d",&*(p+I));
}
printf("\n Elements of array are:);
for(i=0;I<n;i++)
{
printf("%d\n",*(p+i));
  }
}
free(p);
return 0;
}

So here by writing free(p), we released the memory which has dynamically allocated using malloc()

Conclusion:-
So finally our topic is end here, I hope you learn what is Dynamic memory allocation and releasing dynamically allocated memory. If you have any questions or problems related to topic Dynamic memory allocation and releasing dynamically allocated memory then do comments and ask your question in comments section. We reply your answer in just next 24 hours.

Post a Comment

0 Comments