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
3
votes
2 answers
Understanding GCC's alloca() alignment and seemingly missed optimization
Consider the following toy example that allocates memory on the stack by means of the alloca() function:
#include
void foo() {
volatile int *p = alloca(4);
*p = 7;
}
Compiling the function above using gcc 8.2 with -O3 results in…

JFMR
- 23,265
- 4
- 52
- 76
3
votes
1 answer
Why does this CMake script find "alloca" and still fail?
I'm using the alloca function in one of my projects and decided to use CMake to make sure it's available. So I added this bit to my CMakeLists.txt file:
include(CheckSymbolExists)
check_symbol_exists(alloca stdlib.h;cstdlib ALLOCA_EXISTS)
if (NOT…

Dovahkiin
- 946
- 14
- 25
3
votes
0 answers
LLVM def-use for alloca values
I have been exploring def-use for alloca values.
I have the following piece of IR code -
continuation: ; No predecessors!
%iftmp = phi i32 [ 8, %then ], [ 20, %else ]
store i32 %iftmp, i32* %datasize
%res…

mal
- 143
- 2
- 12
2
votes
1 answer
Implementing the defer keyword in C
Golang has a useful language construct called defer which allows the user to postpone a function's execution until the surrounding function returns. This is useful for ensuring that resources are safely destroyed while keeping the…

CodeSurgeon
- 2,435
- 2
- 15
- 36
2
votes
4 answers
If I want a global VLA, could I use alloca() in the main function?
I have a main function for my app, and I allocate, for example, paths to configuration files, etc. Currently I use malloc for them, but they are never freed and always available for use throughout the lifetime of the app. I never even free them…

user16217248
- 3,119
- 19
- 19
- 37
2
votes
1 answer
how does stack growing work on windows and linux?
I just read that windows programs call _alloca on function entry to grow the stack if they need more than 4k on the stack. I guss that every time the guard page is hit windows allocates a new page for the stack, therefore _alloca accesses the stack…

Jochen_0x90h
- 311
- 3
- 5
2
votes
1 answer
Strange assembly code for c alloca function with optimization disabled - gcc uses DIV and IMUL by a constant 16, and shifts?
I have this simple code in c
#include
#include
int main()
{
char* buffer = (char*)alloca(600);
snprintf(buffer, 600, "Hello %d %d %d\n", 1, 2, 3);
return 0;
}
I would expect that generated assembly code for alloca…

Explorer
- 23
- 3
2
votes
2 answers
Run-Time Check Failure #4 - Stack area around _alloca memory reserved by this function is corrupted?
#include
#include
void print_vals(int n)
{
int *arr = (int *)alloca(n);
for (int i = 0; i < n; i++)
arr[i] = i;
for (int i = 0; i < n; i++)
std::cout << arr[i] << ' ';
std::cout <<…

StackExchange123
- 1,871
- 9
- 24
2
votes
1 answer
What's the function of alloca() with setjmp?
This question comes from Practical usage of setjmp and longjmp in C and How to implement coroutine within for loop in c which I asked.
jmp_buf bufferA, bufferB;
void routineB(); // forward declaration
void routineA()
{
int r = 0;
…

JustWe
- 4,250
- 3
- 39
- 90
2
votes
2 answers
emulating `alloca()` in C
If you read through the GNU libs docs, you can see:
Some non-GNU systems fail to support alloca, so it is less portable.
However, a slower emulation of alloca written in C is available for
use on systems with this deficiency.
How would a C…

user1095108
- 14,119
- 9
- 58
- 116
2
votes
3 answers
How to return VLA with size varying on each function instance?
I'm using a nice GCC extensions which allow us to declare VLAs inside structures. For now I found a way to pass VLAs to functions (by value) this way. I also find a way to return one but in a very limited context.
The function code of this example…

AnArrayOfFunctions
- 3,452
- 2
- 29
- 66
2
votes
7 answers
Growing an array on the stack
This is my problem in essence. In the life of a function, I generate some integers, then use the array of integers in an algorithm that is also part of the same function. The array of integers will only be used within the function, so naturally it…

Thomas
- 6,032
- 6
- 41
- 79
2
votes
4 answers
C alloca function - what happens when too much memory is tried to be allocated
In C the alloca() function allocates memory on the stackframe of the caller of alloca().
What happens when you try to allocate a huge number of bytes that it cannot possibly allocate?
Does it allocate as many bytes as it can until the stack meets…

Stefan
- 1,279
- 4
- 14
- 28
2
votes
2 answers
What is the application of alloca?
Possible Duplicate:
In which cases is alloca() useful?
I recently happened to see the use of alloca() function. A google search told me that it's used to allocate space on the stack. I'm not able to grab the application for it ? Also, are there…

KodeWarrior
- 3,538
- 3
- 26
- 40
2
votes
4 answers
Allocation of variable-sized class
I have a variable length data structure, a multi-dimensional iterator:
class Iterator
{
public:
static Iterator& init(int dim, int* sizes, void* mem)
{
return *(new (mem) Iterator(dim, sizes));
}
static size_t alloc_size(int…

marton78
- 3,899
- 2
- 27
- 38