S
Steve Zimmerman
Esteemed contributors to clc:
Thank you for all the responses.
Experiments 2 and 3 below are identical, except that
experiment 2 does not call free(), while experiment 3 does.
With such a trivial program, is it safe not to call free()?
Are there dangers in experiment 2 that experiment 3 avoids?
In what contexts does freeing memory become crucial?
In what contexts does freeing memory not matter?
This is not homework; I have read section 7 of the FAQ.
--Steve
####################### Experiment 2 (malloc) #################
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *h = malloc(30);
if (h == NULL)
fprintf(stderr, "No memory allocation\n");
printf("Successful memory allocation\n");
/* no call to free */
return 0;
}
Output: Successful memory allocation
####################### Experiment 3 (malloc) #################
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *h = malloc(30);
if (h == NULL)
fprintf(stderr, "No memory allocation\n");
printf("Successful memory allocation\n");
free(h); /* call to free */
return 0;
}
Output: Successful memory allocation
################################################################
Thank you for all the responses.
Experiments 2 and 3 below are identical, except that
experiment 2 does not call free(), while experiment 3 does.
With such a trivial program, is it safe not to call free()?
Are there dangers in experiment 2 that experiment 3 avoids?
In what contexts does freeing memory become crucial?
In what contexts does freeing memory not matter?
This is not homework; I have read section 7 of the FAQ.
--Steve
####################### Experiment 2 (malloc) #################
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *h = malloc(30);
if (h == NULL)
fprintf(stderr, "No memory allocation\n");
printf("Successful memory allocation\n");
/* no call to free */
return 0;
}
Output: Successful memory allocation
####################### Experiment 3 (malloc) #################
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *h = malloc(30);
if (h == NULL)
fprintf(stderr, "No memory allocation\n");
printf("Successful memory allocation\n");
free(h); /* call to free */
return 0;
}
Output: Successful memory allocation
################################################################