2024-12-19 10:43 PM
Hi All,
I want to know what is maximum function depth supported by STM32F417 in stmcube ide?
how function depth managed by stack pointer?
Solved! Go to Solution.
2024-12-19 11:31 PM - edited 2024-12-19 11:47 PM
There is no limit. The stack may overflow at run-time and this is not automatically detected and in your responsibility.
Here is a recursive function:
int ackermann(int n, int m) {
if (n == 0)
return m + 1;
else if (m == 0)
return ackermann(n - 1, 1);
else
return ackermann(n - 1, ackermann(n, m - 1));
}
ackermann(3,6) is 509. Calculating this results involves 2.986.490 recursive function calls (without optimizations). Finding the max. stack size used is left an exercise.
Learning abaout the stack in Cortex-M is a covered in books like "Definitve Guide..." by Joseph Yiu and online:
Happy reading
KnarfB
2024-12-19 10:52 PM
This is not exactly "function depth".
But yes, the critical parameter here is stack size, which is part of the specific project settings.
I don't use CubeIDE, so here an example from another toolchain / IDE:
In most cases, stack size is a parameter retrieved or shown during project creation, a.k.a. "project wizard".
Usually a default setting is presented you can edit at that time, or later on.
2024-12-19 11:11 PM
thanks for quick reply,
but my question in not belong to Stack size or stack over flow,
my Questions is -
1. how many C functions can be call in nested form?
2. How stack manage these nested functions by stack pointers
example:
int main()
{
func_1();
}
void func_1();
{
void func_2();
.
.
}
2024-12-19 11:31 PM - edited 2024-12-19 11:47 PM
There is no limit. The stack may overflow at run-time and this is not automatically detected and in your responsibility.
Here is a recursive function:
int ackermann(int n, int m) {
if (n == 0)
return m + 1;
else if (m == 0)
return ackermann(n - 1, 1);
else
return ackermann(n - 1, ackermann(n, m - 1));
}
ackermann(3,6) is 509. Calculating this results involves 2.986.490 recursive function calls (without optimizations). Finding the max. stack size used is left an exercise.
Learning abaout the stack in Cortex-M is a covered in books like "Definitve Guide..." by Joseph Yiu and online:
Happy reading
KnarfB
2024-12-20 12:41 AM
thanks for sharing such informative links....
that will be helpfull
2024-12-20 02:20 AM
> 1. how many C functions can be call in nested form?
As much as the stack size allows.
In general, toolchains/IDEs are really bad at guessing runtime stack sizes. Set to a ball park figure according to code complexity, and increase if you encounter runtime overflows.
Standard library functions (like "printf()") have a significant impact, be aware.
Many RTOSes provide a runtime stack check option for their tasks, as a side note.
2024-12-20 02:39 AM
@vchau.2 wrote:2. How stack manage these nested functions by stack pointers
That part is just standard Computer Science - nothing specific to CubeIDE or STM32.
It isn't even specific to C.
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Compile-time_memory_management