You are on page 1of 1

LAB PROGRAM 3 /*Program to demonstrate malloc(), realloc() and free()*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.

h>

void main() { char *buffer; clrscr(); /*allocating memory*/ buffer=(char *)malloc(10*sizeof(char)); if(buffer==NULL) { printf("Insufficient Memory"); exit(0); } printf("Buffer of size 10 created\n"); strcpy(buffer,"Hyderabad"); printf("\nBuffer contains: %s\n",buffer); /*reallocation*/ buffer=(char *)realloc(buffer,25*sizeof(char)); if(buffer==NULL) { printf("Insufficient Memory"); exit(0); } printf("\nBuffer size modified\n"); printf("\nNew buffer size is 25\n"); printf("\nBuffer still contains: %s \n",buffer); strcpy(buffer,"Secunderabad"); printf("\nBuffer now contains: %s",buffer); /*freeing the memory*/ free(buffer); getch(); } /*to check overflow*/ /*to check overflow*/

You might also like