Difference between revisions of "Dynamic memory allocation in C"
From Embedded Systems Learning Academy
Proj user14 (talk | contribs) (Created page with "Malloc ,Callloc, Realloc, free: 1)Malloc: Malloc dynamically allocates requested memory and returns pointer to it. <syntaxhighlight lang="c"> /* We are allocating memory for...") |
Proj user6 (talk | contribs) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
Malloc ,Callloc, Realloc, free: | Malloc ,Callloc, Realloc, free: | ||
− | 1)Malloc: | + | 1) '''Malloc''': |
Malloc dynamically allocates requested memory and returns pointer to it. | Malloc dynamically allocates requested memory and returns pointer to it. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
Line 8: | Line 8: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | 2)Calloc: | + | 2) '''Calloc''': |
Calloc dynamically allocates requested memory and initializes it to zero. | Calloc dynamically allocates requested memory and initializes it to zero. | ||
Calloc is compiler independent. | Calloc is compiler independent. | ||
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | 3)Realloc: | + | 3) '''Realloc''': |
Realloc is used when we have dynamically allocated memory and need to resize it. | Realloc is used when we have dynamically allocated memory and need to resize it. | ||
− | Realloc can | + | Realloc can be used as a substitute to malloc or free. |
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
− | ( | + | int *ptr1 = (int *) realloc(ptr ,4*n*sizeof(int)); //dynamically allocated memory by malloc at pointer "ptr" will be increased 4 times in size. |
+ | ptr1 = (int *)realloc(ptr1,0); //use realloc as free | ||
+ | int *ptr2 = (int *)realloc(NULL,n*sizeof(int)); //used as malloc | ||
+ | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | 4)free: | + | 4) '''free''': |
− | After use of dynamically allocated memory, free the memory. | + | After use of dynamically allocated memory, free the memory.<br> |
Consider below example: | Consider below example: | ||
− | + | <syntaxhighlight lang="c"> | |
− | // | + | free(ptr); // space pointed by pointer "ptr" is made available but the content in space is unchanged. |
− | |||
− | |||
ptr = NULL;// NULL represents address 0 and it can not be dereferenced so ptr can not be dereferenced. | ptr = NULL;// NULL represents address 0 and it can not be dereferenced so ptr can not be dereferenced. | ||
+ | </syntaxhighlight> |
Latest revision as of 16:15, 27 April 2017
Malloc ,Callloc, Realloc, free:
1) Malloc: Malloc dynamically allocates requested memory and returns pointer to it.
/* We are allocating memory for "n" integers and returns integer pointer */
int *ptr = (int *) malloc(n * sizeof(int)); // We have type casted pointer to int
2) Calloc: Calloc dynamically allocates requested memory and initializes it to zero. Calloc is compiler independent.
/* We are allocating memory for "n" integers and returns integer pointer */
int *ptr = (int *) calloc(n,sizeof(int)); // We have type casted pointer to int
3) Realloc: Realloc is used when we have dynamically allocated memory and need to resize it. Realloc can be used as a substitute to malloc or free.
int *ptr1 = (int *) realloc(ptr ,4*n*sizeof(int)); //dynamically allocated memory by malloc at pointer "ptr" will be increased 4 times in size.
ptr1 = (int *)realloc(ptr1,0); //use realloc as free
int *ptr2 = (int *)realloc(NULL,n*sizeof(int)); //used as malloc
4) free:
After use of dynamically allocated memory, free the memory.
Consider below example:
free(ptr); // space pointed by pointer "ptr" is made available but the content in space is unchanged.
ptr = NULL;// NULL represents address 0 and it can not be dereferenced so ptr can not be dereferenced.