Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

USE OF realloc AND free FUNCTIONS

Posted By: Verina Fischer     Category: C Programming     Views: 2893

Write a program to store a character string in a block of memory space created by malloc and then modify the same to store a larger string.

Code for USE OF realloc AND free FUNCTIONS in C Programming

#include <stdio.h>
#include<stdlib.h>
#define NULL 0
main()
{
    char *buffer;
    /* Allocating memory */
if((buffer = (char *)malloc(10)) == NULL) { printf(“malloc failed.\n”); exit(1); } printf(“Buffer of size %d created \n”,_msize(buffer)); strcpy(buffer, “HYDERABAD”); printf(“\nBuffer contains: %s \n “, buffer); /* Realloction */
if((buffer = (char *)realloc(buffer, 15)) == NULL) { printf(“Reallocation failed. \n”); exit(1); } printf(“\nBuffer size modified. \n”); printf(“\nBuffer still contains: %s \n”,buffer); strcpy(buffer, “SECUNDERBAD”); printf(“\nBuffer now contains: %s \n”,buffer); /* Freeing memory */
free(buffer); } Output Buffer of size 10 created Buffer contains: HYDERABAD Buffer size modified Buffer still contains: HYDERABAD Buffer now contains: SECUNDERABAD
  
Share: 


Didn't find what you were looking for? Find more on USE OF realloc AND free FUNCTIONS Or get search suggestion and latest updates.

Verina Fischer
Verina Fischer author of USE OF realloc AND free FUNCTIONS is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!