what is _alloca()?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
earlz

what is _alloca()?

Post 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
paulbarker

Re:what is _alloca()?

Post 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.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Re:what is _alloca()?

Post 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... ???
earlz

Re:what is _alloca()?

Post 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
paulbarker

Re:what is _alloca()?

Post by paulbarker »

What compiler are you using?
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:what is _alloca()?

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:what is _alloca()?

Post 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().
Every good solution is obvious once you've found it.
Post Reply