Is there a way to tell gcc to allocate local variable on the heap not the default stack?

David Brown david@westcontrol.com
Tue May 29 06:36:00 GMT 2018


On 29/05/18 03:27, Remus Clearwater wrote:
> Hi, for some reason, I want to keep the stack frame usage of some functions
> as smaller as possible, but rewrite them by hands is such a pain and very
> stupid since they are using many and big local variables currently. And in
> these functions they doesn't do anything like longjmp.
> 
> So, is there a option to tell gcc to allocate local variable on the heap
> not the default stack (auto free them before return)?
> 

No, there is no option for that.  (In C++, you can have objects that
manage data on the heap so that the object itself is local on the stack
but the bulk of the storage is on the heap.)

There are some warning options that could help you spot problem
functions, like -Wlarger-than and -Wstack-usage.  You can also generate
stack usage information with -fstack-usage.

Also note that if the objects are not VLAs and you are not using
recursion, you can usually use "static" on the local variables to give
them fixed static addresses (in the data/bss section, rather than the
heap or stack) and thus move them off the stack.

Finally, are you sure the stack usage is actually a problem?



More information about the Gcc-help mailing list