This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: Policy: alloca vs. malloc?
On 8 June 2012 11:54, Paul Eggert <eggert@cs.ucla.edu> wrote:
>> the only
>> 'memory optimization' to speak of here is the malloc chunk overhead on
>> the heap, which isn't much to brag about.
>
> Sorry, I don't follow. ?Both versions use the same amount of heap memory.
> When using the stack, the alloca version allocates fewer bytes than the
> non-alloca version. ?Granted, this is not a huge amount of memory savings
> (it's bounded by the size of the local array) but it does save some space.
>
> I tried to clarify this by changing the wording to the following:
>
> ? Use of {{{alloca}}} is a memory optimization. That is, the above example
> ? is close in behavior to the following, except that the {{{alloca}}}
> ? version consumes only the stack space needed, rather than always consuming
> ? approximately 4000 bytes on the stack.
I thought you meant memory optimization to be alloca vs malloc when
you could have done a malloc straight away, which is why I mentioned
the chunk overhead being the only saving. Your clarification above
seems to imply savings due to doing alloca when one could have
declared a local array of the max size. Is that what it is? It
probably needs to be even clearer, something like:
Use of {{{alloca}}} is a memory optimization compared to having a
local array on stack...
--
Siddhesh Poyarekar
http://siddhesh.in