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
7
votes
1 answer
How to tell LLVM that it can optimize away stores?
Background (there may be a better way to do this):
I am developing a Julia library in which I manually manage memory; I mmap a large block, and then mostly treat it like a stack: functions receive the pointer as an argument, and if they allocate an…

Chris Elrod
- 357
- 2
- 8
7
votes
2 answers
Why can I goto into the scope of a alloca:d variable, but not a variable length array?
See this test program:
#include
#include
int main(int argc, char *argv[])
{
if (argc < 2)
goto end;
char s[strlen(argv[1]) + 1];
strcpy(s, argv[1]);
printf("s=%s\n", s);
end:
return 0;
}
It fails to compile…

Tor Klingberg
- 4,790
- 6
- 41
- 51
6
votes
3 answers
Access violation when using alloca
My stackAlloc function looks like this:
void* stackAlloc(size_t size) {
if (size > maxStackAllocation)
return malloc(size);
else
return _alloca(size);
}
void stackAllocFree(void *ptr, size_t size) {
if (size >…

hidayat
- 9,493
- 13
- 51
- 66
5
votes
6 answers
Stack allocation in function wrapper / alloca in function
I'm looking for a way to wrap stack allocations in abstract data types. For example, I'd like to have a vector which can work strictly via allocations on the stack. My biggest hurdle of course is that alloca works only within the current stack frame…

edA-qa mort-ora-y
- 30,295
- 39
- 137
- 267
5
votes
12 answers
Is it possible to predict a stack overflow in C on Linux?
There are certain conditions that can cause stack overflows on an x86 Linux system:
struct my_big_object[HUGE_NUMBER] on the stack. Walking through it eventually causes SIGSEGV.
The alloca() routine (like malloc(), but uses the stack,…

Tom
- 10,689
- 4
- 41
- 50
5
votes
1 answer
Why can not alloca be used in function argument list?
Quoting the second paragraph of BUGS section, from manpage of alloca(3)
On many systems alloca() cannot be used inside the list of arguments of a function call, because the stack
space reserved by alloca() would appear on the stack in…

Naitree
- 1,088
- 1
- 14
- 23
5
votes
1 answer
Fixed size array vs alloca (or VLAs)
When is alloca() preferable to memory allocated on the stack by declaring a fixed size array?
Details:
As we know, alloca() is a controversial function. Used recklessly, it can cause stack overflow. Used judiciously, it can shave a few nanoseconds…

Praxeolitic
- 22,455
- 16
- 75
- 126
5
votes
4 answers
How to GCC compile without _alloca?
For some reason, I should use gcc to compile a C file, then link against Visual C++ 2008 project.
(I used the current latest gcc version: cygwin gcc 4.3.4 20090804.)
But there is one problem: gcc always allocate a big array with _alloca,
and VC…

shkim
- 1,173
- 3
- 16
- 24
5
votes
3 answers
Is this a good reason to use alloca?
I have the following function:
double
neville (double xx, size_t n, const double *x, const double *y, double *work);
which performs Lagrange interpolation at xx using the n points stored in x and y. The work array has size 2 * n. Since this is…

Alexandre C.
- 55,948
- 11
- 128
- 197
4
votes
2 answers
Why does `alloca` not check if it can allocate memory?
Why does alloca not check if it can allocate memory?
From man 3 alloca:
If the allocation causes stack overflow, program behavior is undefined. … There is no error indication if the stack frame cannot be extended.
Why alloca does not / can not…
user4385532
4
votes
1 answer
What's up with gcc's handling of alloca?
On most platforms, alloca just boils down to an inline adjustment of the stack pointer (for example, subtracting from rsp on x64, plus a bit of logic to maintain stack alignment).
I was looking at the code that gcc generates for alloca and it is…

BeeOnRope
- 60,350
- 16
- 207
- 386
4
votes
6 answers
In which cases is alloca() useful?
Why would you ever want to use alloca() when you could always allocate a fixed size buffer on the stack large enough to fit all uses? This is not a rhetorical question...

zr.
- 7,528
- 11
- 50
- 84
3
votes
2 answers
When is memory allocated using alloca freed for class members?
class MyString
{
public:
MyString(int length):_ptr(alloca(length))
{
}
//Copy Constructor, destructor, other member functions.
private:
void* _ptr;
};
int main()
{
MyString str(44);
return 0;
}
Is it freed at the end of the main…

balki
- 26,394
- 30
- 105
- 151
3
votes
3 answers
Why is alloca returning the same address twice?
I'm trying to implement my own math library, and I'm starting off with vectors. The idea is to give the class a pointer to an array of numbers, then copy the array and store it in the data address given by a private variable pointer. To begin with,…

Joshua Pasa
- 97
- 7
3
votes
3 answers
Constructing a function pointer to alloca causes linker errors?
I am trying to write a function that is passed a function to use for allocation as its argument; it should accept any valid allocator of type void *(*)(size_t). However I am experiencing strange behavior when attempting to use alloca as the…

l k
- 117
- 8