Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!zaphod.mps.ohio-state.edu!mips!prls!philabs!linus!linus!ccels3.mitre.org!rtidd From: rtidd@ccels3.mitre.org (Randy Tidd) Newsgroups: comp.lang.c Subject: Question about malloc() and free() Message-ID: <118073@linus.mitre.org> Date: 24 Aug 90 18:48:23 GMT Sender: usenet@linus.mitre.org Reply-To: rtidd@ccels3.mitre.org (Randy Tidd) Organization: The Mitre Corporation Lines: 42 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@mwunix.mitre.org FAST #define DISCLAIM TRUE CHEAP -pick any two