Re: Man Page `alloca()`: Important Info Is Missing?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Robin,

On 10/13/2018 11:46 AM, Walter Harms wrote:
> 
> 
>> Robin Kuzmin <kuzmin.robin@xxxxxxxxx> hat am 10. Oktober 2018 um 23:40
>> geschrieben:
>>
>>
>> http://man7.org/linux/man-pages/man3/alloca.3.html
>>
>> I compare this man page with the
>> https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/alloca?view=vs-2017
>> and I feel that a very important fragment is missing in the man page
>> (although strictly speaking the man page is complete), the missing
>> fragment is between the double asterisks (**):
>>
>> The allocated space is automatically freed when the calling function
>> exits **(not when the allocation merely passes out of scope)**.
> 
> My Version reads:
> "This temporary space is automatically  freed  when  the function that called
> alloca() returns to its caller."
> I thing this is pretty clear.

I'm inclined to agree with Walter actually. And indeed I find
the text that you quote is actually a bit confused. The *allocation*
doesn't pass out of scope; the *pointer* to the allocation might pass
out of scope.

>> More info:
>> * Know the Danger of alloca()
>> https://github.com/kuzminrobin/code_review_notes/blob/master/cpp_design_bookmarks.md#know-the-danger-of-alloca

There, you say (I assume s/malloca/alloca/):

    Thus if the malloca() is called in the loop then each loop 
    iteration allocates more and more space on the stack which 
    can easily cause the stack overflow.

    Such a behavior is fundamentally differnt from the 
    behavior of its counterparts - the ordinary arrays (whose 
    number of elements is specified with a compile-time constant),
    and the variable length arrays (whose number of elements is 
    specified with a run-time value). Both of these couterparts
    reuse the space on the stack during each iteration of the loop.

But, this is to me a slightly surprising way of thinking
about alloca(). To me, the counterparts for behavior are malloc()
etc. And if you used malloc() in the same way, again you would
have a memory issue (but on the heap, rather than the stack).

So, I'm not saying you're wrong in any of your assertions, it's
just that (to me) it's a surprising way of (mis)understanding alloca().

But, perhaps some people do (mis) understand alloca() as you suggest,
so I've added the following sentence in NOTES:

       The space allocated by alloca() is not automatically deallocated
       if the pointer that refers to it simply goes out of scope.

Cheers,

Michael



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/



[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux