Traduisez - Übersetzen - Traduzca - Traduza - Tradurre - Translate

VanLUG Email Archive

VanLUG Mailing List
Memory allocation in ANSI C (Re: Memory allocation under Linux)

New Message Reply About this list Date view Thread view Subject view Author view

Kaz Kylheku (kaz@cafe.net)
Sat, 16 Jan 1999 10:59:50 -0800


> On Fri, 15 Jan 1999, Shane Wegner wrote:
>
> > I have a few questions with Memory allocation under Linux and am hoping
> > someone here has some experience with it. In particular malloc(2) and
> > free(2) questions. In the following function, does s free itself?
> >
> > int test(void) {
> > char *s;
> > s = malloc(1024);
> > return 0;
> > }
>
> Nope. malloc() wouldn't be of much use if it did.

Actually such a function would be quite useful, which is why there exists an a
function called ``alloca'', whose interface is just like that of malloc() but
which allocates storage that is disposed of when the function terminates. (For
example, it may get the storage by extending the stack frame. This is the main
raison d'etre for alloca: extending a stack frame is darn fast compared to
using a dynamic allocator. Of course, with alloca you must not try to export
the allocated memory out of the function. The amount you can allocate may be
more limited than with malloc(); in Linux, the default stack size is 8 megs
(less if you are using threads; LinuxThreads have a 2 megabyte stack each, I
believe).

[[
Sadly, alloca wasn't incorporated into ANSI C because of the difficulties it
could pose to some implementations. I haven't looked at the Rationale in a
while, but I think it mentions it. One possible reason may be that some C
implementations may which allocate and deallocate storage for each statement
block rather than in one swoop at the start of a function. There could be
harmful interference between this management of automatic storage and an
implementation of alloca which allocates from the same automatic storage.
Implementing alloca using dynamic storage, on the other hand, would require
these implementors to add a mechanism not unlike C++ destruction, which would
chase down all of the memory. Or a completely separate stack might have to be
maintained for alloca(). Anyway, since I've written this much about it already,
I might as well dig up the reference:

        Some implementations provide a function (often called alloca) which
        allocates the requested object from automatic storage; the object is
        automatically freed when the calling function exits. Such a function is
        not efficiently implementable in a variety of environments, so it was
        not adopted in the Standard. (ANSI C Rationale, 4.10.3)
]]

Anyway, this isn't a Linux-specific topic. Few, if any, C implementations
do automatic garbage collection of memory created with malloc().

Here are some resources for C programming, not specific to Linux.
First, there is a great C page here:

        http://www.lysator.liu.se/c/index.html

Then there is the home page of the comp.lang.c FAQ maintainer:

        http://www.eskimo.com/~scs

The VANLUG list isn't the best place for generic C questions, though it
might not be the worst of places in terms of available talent. :)


New Message Reply About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b3 on Sat 16 Jan 1999 - 10:04:30 PST