By Dan Gookin

It’s not as much of an issue as it was back in the microcomputer era, but wasting memory should still be a concern for any C programmer. Though you can brace yourself for 1,024 characters of input, odds are good that your program’s users may not all be Stephen King.

In that case, you can pare down your memory requests after you make them. The extra memory can then be returned to the operating system in what’s considered common courtesy.

GIVING BACK A FEW BYTES

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *input;
int len;
input = (char *)malloc(sizeof(char)*1024);
if(input==NULL)
{
puts("Unable to allocate buffer! Oh no!");
exit(1);
}
puts("Type something long and boring:");
fgets(input,1023,stdin);
len = strlen(input);
if(realloc(input,sizeof(char)*(len+1))==NULL)
{
puts("Unable to reallocate buffer!");
exit(1);
}
puts("Memory reallocated.");
puts("You wrote:");
printf("%s"n"