Documentation of alloca() in libc
Adhemerval Zanella
adhemerval.zanella@linaro.org
Mon Aug 24 16:02:47 GMT 2020
On 24/08/2020 12:35, Philip R Brenan via Libc-alpha wrote:
> Hi *LibC*:
>
> Given the example for *alloca*() at:
>
> https://www.gnu.org/software/libc/manual/html_mono/libc.html#Alloca-Example
>
> May I recommend that the code below portrays the use of alloca() more
> completely in that it demonstrates that it is exiting the containing
> function, not the {} of the first for loop that frees the storage allocated
> by alloca()?
>
> #include <stdlib.h>
> #include <stdio.h>
>
> int main(void)
> {const int N = 26;
> char *(p[N]);
> for(int i = 0; i < N; ++i) {*(p[i] = alloca(1)) = 'a'+i;}
> for(int i = 0; i < N; ++i) putchar(*(p[i]));
> return 0;
> }
>
> // abcdefghijklmnopqrstuvwxyz
>
Is the idea to outline that alloca allocated memory holds for function scope
instead of function scope? I think you can outline it with another example,
however it is already explicit state on alloca ('all the blocks are freed
when you exit the function that alloca was called from') and the example idea
imho is to state one does not need to handle memory deallocation on error path
or non-local exit.
But fell free to send a documentation patch, the only thing I think it should
add is the alternate version with malloc that require much more boilerplate.
More information about the Libc-alpha
mailing list