Link to home
Start Free TrialLog in
Avatar of ylucki
ylucki

asked on

alloca()


I was debuggin some old code in my project and come across the following lines of code.

            void* _obj;
           _obj = alloca(32768); /* Trick to make the compiler not to mess up with stackframs */

Does it make any sense to any of you ? Is it 64bit safe on solaris ?

Regards,
Lucky.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


It would be interesting to see how _obj is used later in the function.  :)

By reserving space on the stack instead of the heap, the programmer is ensuring that the buffer will always get free()d when the function exits.  Resetting the stack pointer when the function exits is just a couple of instructions and therefore very, very fast.  Executing the free() function is much slower.

So by using alloca() instead of malloc() the programmer is writing a function that can not possibly have a memory leak (which can greatly simplify some functions) and will have a shorter run time.

But unless I were coding a VERY time-critical operation I would stay away from this kind of practice.

Kent
Avatar of ylucki
ylucki

ASKER


Well..yup !! It is very time-critical operation.
But _obj is not used any where in the function, after allocating memory.

I just wonder what the significance of 32768 here could be. Is it 64bit safe on solaris ?

Actually my application crashes when run in 64-bit mode. I just wonder if this could be one of the reasons.

-Lucky
As I see it, the only reason to use alloca is if the size to be allocated is variable.

Using....
--------8<--------
{
            void* _obj;
           _obj = alloca(32768); /* Trick to make the compiler not to mess up with stackframs */
}
--------8<--------
...is inherently slower than...
--------8<--------
{
char _obj[32768];
}
--------8<--------
...because the alloca function needs to do a lot of inline messing around (e.g. saving the stack pointer - I've not looked at the GCC disassembly of this but that's certainly true of Visual C's _alloca implementation - and certainly a function call anyhow). There is no need to use it if you are alloca-ing a constant amount of stack.

Your crash could be because you ran out of stack as a consequence of putting too much data onto it. Unlike the heap, stack size is a fixed allocation predetermined when your program is linked in Windows, predetermined by the kernel in Linux for normal applications (see _STK_LIM in /usr/src/linux/include/linux/sched.h) or determined when a thread is created by the attributes passed to pthread_create if you are using POSIX threads. When you run out of stack space, your program goes kaboom.

Disclaimer: I only recently became aware of alloca. The opinions stated here are those of an over-opinionated newbie :-)
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good point - it bypasses half of the space addressable by SS:SP in DOS-ville of you increment SP by that amount, if SS is unaltered. [0x8000]
Avatar of ylucki

ASKER


thanks for the information !

well....i would love to modify the code....but it is generated code and we don't have control on the code :-(

will update if something pops up in our further investigation !

thanks,
-lucky.