scope of malloc

Bob Martin rmartin at clear.com
Sat Nov 10 01:54:28 AEST 1990


In article <1791 at sirius.ucs.adelaide.edu.au> mferrare at 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 at clear.com    | My opinions are mine.  They aren't anybody  |
| uunet!clrcom!rmartin | elses.  And thats the way I want to keep it.|
+----------------------+---------------------------------------------+



More information about the Comp.lang.c mailing list