Memory allocation

Dynamic memory allocation:
  • Dynamic memory allocation means, a program can obtain its memory while it is running.
  •  It allows us to allocate additional memory space or release unwanted space (i.e. previously allocated) at the time of program execution (runtime).
  • The dynamic memory allocation is efficiently allowed to share the memory.
  • It is very useful to modify the size of the previous allocated memory.
Static memory:
  • So far, we have used static memory. Static memory means which reserve a certain amount of memory by default inside the program to use for variables and such.
  • It means that once reserve this memory, no other the program can use it, even if we are not using it at the time.
Dynamic memory allocation function:
  • Pointers support the dynamic memory allocation in ‘c’ language.
  • The ‘C’ language provides four library functions known as ‘Memory Management Function’ which can be used for allocating and realising memory during execution.
  • Any program, which uses these below functions, must be including the header file.
                                      #include<stdlib.h>
Example program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char *p;//pointer variable
clrscr();
p=(char *) malloc(6);//allocate memory space to variable
strcpy(p,"MADRAS");
printf("\n Memory contains: %s\n",p);
p=(char *) realloc(p,7);//reallocation
strcpy(p,"CHENNAI");//string cpoy
printf("\n Memory now contains: %s\n",p);
free(p);// releasing memory
getch();
}

Output:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me