richard_damon wrote on Saturday, July 23, 2016:
The one thing that has problems with automatic (stack based) variables retaining values would be inside co-routines which are a very striped down version of tasks for use in very memory limited machines. As MEdwards states, tasks have no problem retaining values over a context switch.
A Task would have problems remembering values if the task ends and gets restarted, then variables on the stack would likely lose their value.
As far as when to use the stack and when to use local static variables, if the function is used for only 1 task, it makes little difference. If the function is used for multiple tasks, like the function supports multiple devices in the system, and the parameter passed on task startup defines which one that task is handling, then stack variable would be distinct for each class, while a static variable would be shared.
Which is better? Using stack variables makes it easier if later you want to reuse the function for a second task. Static variables make it easier for sub-functions to access the values.
As to the maximum size of the tasks stack, one answer is how much you allocated, as that is the most it is allowed to use. As to how much it will use, some compilers have an option to try and compute the stack usage for a function (and thus a task), others do not, and you would have to estimate/count how much stack is used for variables and add for parameter/function calls and other overhead.