Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!rutgers!mcdchg!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c Subject: Re: scope of malloc Message-ID: <1990Nov9.145428.16417@clear.com> Date: 9 Nov 90 14:54:28 GMT References: <1791@sirius.ucs.adelaide.edu.au> Organization: Clear Communications, Inc. Lines: 21 In article <1791@sirius.ucs.adelaide.edu.au> mferrare@adelphi.ua.oz.au (Mark Ferraretto) writes: >You see, I have this question... > When malloc is called in a function and the scope of the pointer is >only that function is the memory deallocated when the pointer is >(ie on exit of the function?) deallocated? The "scope" of memory allocated by malloc is the same as the scope of a static variable. The memory lasts until one of two events: 1) you explicitly de-allocate it with the 'free' function. 2) The program (main) exits or aborts. It is possible to allocate memory which DOES get deallocated when the allocating function returns. You must use the function 'alloca' which allocates the memory on the stack. However use of this function may not be portable. -- +-Robert C. Martin-----+---------------------------------------------+ | rmartin@clear.com | My opinions are mine. They aren't anybody | | uunet!clrcom!rmartin | elses. And thats the way I want to keep it.| +----------------------+---------------------------------------------+