Question about malloc() and free()
Randy Tidd
rtidd at ccels3.mitre.org
Sat Aug 25 04:48:23 AEST 1990
I ran into what I think is an interesting question, I hope someone can
help me out. I hope this hasn't been beaten to death around here, but
I haven't seen it recently.
Say I have a routine that allocates big block of memory and returns a
pointer to it, something like:
char *get_chars()
{
char *big_block;
int i;
big_block = malloc(10001);
for(i=0; i<10000; i++)
big_block[i] = getchar();
big_block[i] = '\0'; /* NULL-terminate the array */
return(big_block);
]
And the calling function does something like:
main()
{
char foo[10001];
strcpy(foo,get_chars());
printf("%s\n",foo);
}
What happens to the space that was malloc'ed? It is never freed by me;
will the compiler figure this out and arrange for it to be freed? Is
the resolution method standard over compilers?
While this is an extreme case, I ran into something more moderate and
am curious, for performance reasons, of the turnout.
Any help is appreciated. Please e-mail me if you feel the rest of The
Net doesn't want to hear about it.
Randy Tidd GOOD
rtidd at mwunix.mitre.org FAST
#define DISCLAIM TRUE CHEAP
-pick any two
More information about the Comp.lang.c
mailing list