Page 1 of 1

what is _alloca()?

Posted: Fri Mar 31, 2006 11:08 pm
by earlz
I was trying to add some c++ to my kernel and got a linker error for _alloca so what is it, the parameters, and is there a dealloca because i used my generic sbrk() as alloca but it didn't give me an error and i have no dealloc

Re:what is _alloca()?

Posted: Sat Apr 01, 2006 3:54 am
by paulbarker
alloca should allocate memory on the stack, so that when the function returns the memory is automatically freed.

Now, the question of how thats done is not something I can answer. Its usually compiler magic.

Re:what is _alloca()?

Posted: Sat Apr 01, 2006 7:58 am
by nick8325
It's in the FAQ :)

How kernel, compiler, and C library work together (near the bottom)

It'll appear if you have a function with more than 4KB of local variables, or (possibly) if you have a local variable which doesn't have a constant size (e.g. an array with a variable sized number of elements).

You might be able to find a proper implementation (for GCC) in libgcc, but I can't find it on the GCC website any more... ???

Re:what is _alloca()?

Posted: Sat Apr 01, 2006 12:27 pm
by earlz
I understand what it does now but how the crap would you make it and how is the memory automatically freed at the exit of a function without calling another function to free

btw
The alloca function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged.
so is there anyway to avoid using it

Re:what is _alloca()?

Posted: Sat Apr 01, 2006 12:57 pm
by paulbarker
What compiler are you using?

Re:what is _alloca()?

Posted: Sat Apr 01, 2006 1:03 pm
by Colonel Kernel
AFAIK, you would implement it on x86 just by subtracting a certain amount from esp. When the function returns, it will restore the stack pointer of the calling frame, thus "freeing" the alloca'd memory just like local variables are freed.

Re:what is _alloca()?

Posted: Sun Apr 02, 2006 1:31 am
by Solar
Jordan3 wrote: so is there anyway to avoid using it
A lot of effort has gone into the FAQ. Many questions are answered there - including those about alloca().