Available stack space at runtime?

Is it possible to determine the bytes of stack space available at runtime. I am thinking about an embedded case but it would likely be useful for desktop use too. This could optimally determine how much data to request from an sd card for batch processing, for example?

I think the only language defined thing you have that I could find is the attribute 'Storage_Size for the current task you are in. However, it is implementation defined if that number is stack only or stack plus other stuff. You might have to look at the gnat docs to see what they do with it:


For a prefix T that denotes a task object (after any implicit dereference):

60/3 T’Storage_Size

Denotes the number of storage elements reserved for the task. The value of this attribute is of the type universal_integer. The Storage_Size includes the size of the task’s stack, if any. The language does not specify whether or not it includes other storage associated with the task (such as the “task control block” used by some implementations.) If the aspect Storage_Size is specified for the type of the object, the value of the Storage_Size attribute is at least the value determined by the aspect.

It’s probably safe to use half that value as a max block size as long as you aren’t doing crazy stuff in the current task like recursive function calls, but you’d obviously want to pre test it out.

1 Like

I shall look into that. Thank you

FreeRTOS doesn’t provide this out of the box, but it shouldn’t be too hard to implement.

I have never done or needed that, so take the following with a grain of salt.

I think I would do the opposite: configure the stack size at compilation time (see 3. The Primary and Secondary Stacks — GNAT User's Guide Supplement for Cross Platforms 24.0w documentation), and use that value in your program. The page I link does mention the Storage_Size attribute, so @jere’s message is likely a good approach too.

1 Like

Thanks for the link. It is less of a need and more of a thought that it would play to Adas strengths such as it’s array support, to be honest.