Showing posts with label realloc. Show all posts
Showing posts with label realloc. Show all posts

Monday 1 August 2016

Dynamic Memory Allocation

Module 1: Dynamic Memory Allocation


  • free()

realloc() Dynamic Memory Allocation

Dynamic Memory Allocation: realloc() 



Download the Program
Syntax:
ptr = (cast_type *)realloc(ptr, newsize_in_bytes);

#include<stdio.h>
void main()
{

       char *str;
       str = (char *)malloc(10);

       if(str == NULL)
      {
          printf("\nInsufficient memory allocation");
          exit(0);
      }

      strcpy(str, "hello");
      printf("\nString is = %s", str);

      str = (char  *)realloc(str, 20);

      if(str == NULL)
      {
          printf("\nInsufficient memory allocation");
          exit(0);
      }

      strcpy(str, "how are you");
      printf("\nString is = %s", str);
      free(str);
}

Output:
String is = hello
String is = how are you

Also Check,
https://tejaswinihbhat.blogspot.in/2016/08/dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/malloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/calloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/realloc-dynamic-memory-allocation.html