Dynamic memory allocation:-

  • void *mallc(size_t size);
  • void *calloc(size_t n, size_t size);
  • void *realloc(void *ptr, size_t size);
  • void *alloca(size_t size);
  • void free(void *ptr);
  • void *memset(void *s, int c, size_t n);
  • int or void *memcmp(const void *s, const void *d, size_t n);
  • void *memcpy(void *dest, const void *src, size_t n);
  • void *memchr(const void *s, int c, size_t n);

Important points:-

  • malloc(), calloc() and realloc gets the memory on the heap.
  • In case of static memory allocation, memory is allocated on the stack.
  • We can not change a size of a statically allocated memory.
  • We also allocate a memory dynamically (at run time) on stack in runtime by alloca() function.
  • alloca() allocates a memory at runtime at a stack.

malloc() (Memory allocation):-

void *mallc(size_t size);

int fun()

{

int *p = (int *)malloc(40);

}

  • malloc() returns a void pointer which is starting address of our dynamically allocated memory.
  • Due to this return data type we cannot access dynamically allocated memory and also we cannot move inside the dynamically allocated memory.
  • To avoid above restriction we have to type cast with appropriate datatype.
  • After type casting we can access particular no. of bytes from that dynamically allocated memory.
  • If malloc(), calloc() and realloc() fails to allocate a memory dynamically in that case all the functions returns NULL.
  • Then it is necessary that check whether the pointer is NULL or not.

e.g.

int *p = (int *)malloc(40);

if(p==NULL)

{

printf(“enable to allocate a memory.”);

}

  • malloc() is a library function which internally calls BRK and SBRK system calls and that system calls internally calls grow rage algorithm.
  • BRK means breaking and SBRK means Segment breaking.
  • malloc() allocates a memory which contains default garbage values but nowadays malloc() also allocates a memory which is initialized with 0.

calloc() (Calculated allocation):-

void *calloc(size_t n, size_t size);

  • calloc() is used for allocating a memory for an array.
  • 1st parameter of calloc() is no of elements of that array and
  • 2nd parameter of calloc() is size of each element of that array.
  • If we have to allocate a memory for int array having 10 members in that case calloc() can be returned as,

int *p = (int *)calloc(10,sizeof int);

int *p = (int *)malloc(10*sizeof int);

  • calloc() allocates a memory which is initialized with 0;

realloc() (Reallocation of memory):-

void *realloc(void *ptr, size_t size);

  • realloc() is used for reallocating a memory which is previously allocated by mallo(), calloc() and realloc() itself.
  • realloc() is used for decrementing the size of a memory or incrementing a size of a memory.
  • 1st parameter of realloc() is pointer of previously allocated memory and
  • 2nd parameter of realloc() is new size.
  • This is not a difference between previous and new size.
  • If 1st parameter of realloc() is NULL then it is worked as malloc().
  • If 2nd parameter of realloc() is 0 then it is worked as free().
  • While reallocating by realloc() if there is no sufficient amount memory in that case memory manager allocates a new memory of appropriate size and copies the previously allocated data into new memory.
  • This condition is only happened when there is no sufficient amount of memory after previously allocated memory.
  • In this case realloc() internally calls memcpy() to copies the contents from one memory location to another memory location.

int *p = (int *)malloc(40);

p = (int *)realloc(p,170);

  • After reallocating the memory and some another location memory manager frees the previously allocated memory.
  • While reallocating if there is no sufficient amount of memory in that case realloc() returns NULL.
  • Due to which we lost previously allocated memory to avoid that drawback we can use some another pointer for receiving the address of newly allocated memory by realloc().

int *p, *q;

p = (int *)malloc(40);

q = realloc(p,40);

  • In this case p must be return an address or output of maooc(), calloc() or realloc().
  • If 1st parameter of realloc is NULL.

 

Introduction to C

 

e.g.

int *p = (int *)realloc(NULL,140);

  • In this case new 140 byte memory is allocated by realloc() and strting addres of that memory is assigned into p.
  • It works similar as malloc().
  • If 2nd parameter of realloc() is 0;

e.g.

int *p = (int *)malloc(40);

p = realloc(p,0);

  • In this case memory allocated by malloc() is freed by realloc().
  • realloc() is also used for decreasing an allocated memory.

e.g.

char *p = (char *)calloc(40,sizeof char);

p = realloc(p,10);

alloca():-

void *alloca(size_t size);

  • To allocate a memory in a stack we use the alloca().
  • After using alloca() we cannot use realloc().
  • alloca() allocates a memory which contains default garbage values.
  • If alloca() fails to return memory then it returns NULL.
  • We cannot allocate a memory from stack to heap so we use alloca().

free():-

void free(void *ptr);

  • free() is used for de allocating a memory which is previously allocated memory by malloc(), calloc() and realloc().
  • This function accepts the starting address of our previously allocating memory and function returns nothing.
  • Parameter of free() must be base address of allocated memory.
  • free() internally removes the memory from used list of memory table and insert it into free list of memory table.
  • After de allocating a memory by using free() we cannot access that memory.

int *p = (int *)malloc(40);

free(p);

printf(*p); // segmentation fault

Example of dynamic memory allocation for 1D and 2D array:-

1D array:-

int *p = NULL;

int no = 0,I = 0;

printf(“enter the elements”);

scanf(“%d”,&no);

p = (int *)malloc(sizeof(int)*no);

printf(“Enter the values”);

for(i=0;i<no;i++)

{

scanf(“%d”,&p[i]);

}

for(i=0;i<no;i++)

{

printf(“%d”,p[i]);

}

free(p); // freed allocated memory.

2D array:-

int **p = NULL;

int row,col,I,j;

printf(“enter the rows and cols”);

scanf(“%d%d”,&row,&col);

p = (int **)malloc(sizeof(int *)*rows);

for(i=0;i<row;i++)

{

for(j=o;j<col;j++)

{

scanf(“%d”,&p[i][j]);

}

}

printf(“elements of an array are”);

for(i=0;i<row;i++)

{

for(j=o;j<col;j++)

{

printf(“%d”,p[i][j]);

}

}

// frees the allocated memory

for(i=0;i<row;i++)

{

free(p[i]);

}

free(p);

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment