Alloca is a function to allocate dynamically memory on the Stack in C. Such memory will be automatically deallocated when leaving the function.
Questions tagged [alloca]
87 questions
492
votes
24 answers
Why is the use of alloca() not considered good practice?
alloca() allocates memory on the stack rather than on the heap, as in the case of malloc(). So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up dynamically allocated memory. Freeing of memory…

Vaibhav
- 5,749
- 3
- 22
- 18
45
votes
4 answers
How does alloca() work on a memory level?
I'm trying to figure out how alloca() actually works on a memory level. From the linux man page:
The alloca() function allocates size bytes of space in the stack
frame of the caller. This temporary space is automatically freed
when the function…

glades
- 3,778
- 1
- 12
- 34
40
votes
3 answers
alloc, malloc, and alloca — What's the difference?
I was under the impression that alloc in Objective-C (when we invoke [anyObject alloc] is actually implementing C function malloc and the memory getting allocated in heap, but could not find anywhere the answer for this.
Also, while searching for…

Anindya Sengupta
- 2,539
- 2
- 21
- 27
36
votes
12 answers
Alloca implementation
How does one implement alloca() using inline x86 assembler in languages like D, C, and C++? I want to create a slightly modified version of it, but first I need to know how the standard version is implemented. Reading the disassembly from…

dsimcha
- 67,514
- 53
- 213
- 334
22
votes
2 answers
Header alloca.h in Windows
I can't see any alloca.h equivalent in Visual C 2010. How can one perform stack allocation in Visual C on Windows? I miss the function alloca.

Cartesius00
- 23,584
- 43
- 124
- 195
20
votes
2 answers
11
votes
3 answers
alloca function in C
I was revising C and came across to alloca/free functions which is described as allocating storage on a stack like space. Is this same as the malloc/free ? or this is something different ? Thanks.

Cemre Mengü
- 18,062
- 27
- 111
- 169
11
votes
2 answers
Is there an allocator that uses alloca and is otherwise C++ STL compliant?
I have two questions:
1) Is it possible to implement an allocator that uses alloca to allocate memory on the stack and is otherwise C++ STL compliant?
If there is code out there, you can make me happy by simply pointing me to the URL. :-)
If there…

Bjoern
- 119
- 3
11
votes
2 answers
What is the purpose of the %"alloca point" line which occurs in llvm code?
I've been looking at some LLVM assembly produced by llvm-gcc lately and I've noticed a recurring statement of which I'm not sure its purpose.
For example, the following C program:
int main(void)
{
void (*f)(void) = (0x21332);
f();
}
When…

David Terei
- 1,985
- 16
- 18
10
votes
1 answer
can we use alloca() or variable length array extentions in c++20 coroutines?
The GCC C++ compiler (any many other C++ compilers as well) provide nonstandard extentions such as
alloca() for stack based allocation
variable length arrays, as they are part of the C standard
Can these be used inside of C++20 coroutines from a…

Andreas H.
- 5,557
- 23
- 32
10
votes
2 answers
Is alloca completely replaceable?
I've read quite a few places that alloca is obsolete and should not be used and Variable Length Arrays should be used instead.
My question is this: Is alloca completely replaceable by variable length arrays?
In my particular instance I have…

Earlz
- 62,085
- 98
- 303
- 499
9
votes
2 answers
Can alloca() memory be reallocated?
Memory allocated by malloc can be reallocated with realloc. Is there a similar function for alloca? Reallocating stack memory could be useful when you don't want memory to be allocated on the heap, and you need to allocate variable stack memory…

Phridge
- 148
- 8
9
votes
2 answers
Is it a good practice to hide structure definition in C?
In my opinion, hiding the definition of a structure in C generally makes code safer, as you enforce—with the help of the compiler—that no member of the structure can be accessed directly.
However, it has a downside in that a user of the structure…

Alexander Solovets
- 2,447
- 15
- 22
9
votes
6 answers
What's the difference between alloca(n) and char x[n]?
What is the difference between
void *bytes = alloca(size);
and
char bytes[size]; //Or to be more precise, char x[size]; void *bytes = x;
...where size is a variable whose value is unknown at compile-time.

Jared Pochtar
- 4,925
- 2
- 29
- 39
9
votes
3 answers
Is a goto in alloca's function scope valid?
The C standard prohibits a goto into a function scope where a VLA exists.
A VLA and the call to alloca function should have the same result on low level.
(I could be wrong, as I'm just a C, not a low level programmer, but in my imagin that appears…

dhein
- 6,431
- 4
- 42
- 74