In a Linux process (or other operating system), when a subroutine is called, the memory for local variables comes from the process stack area. Any dynamically allocated memory (using malloc, new, etc.) comes from the process heap area. During recursion, local memory is allocated from the stack area during a function call and is cleared when the function is executed.
The lowest address located at the bottom is displayed in the memory, and the top is at the top. Below are the steps to find the direction of stack growth in recursion using the fast C code.
#include <stdio.h> void test_stack_growth_direction(recursion_depth) { int local_int1; printf("%p\n", &local_int1); if (recursion_depth < 10) { test_stack_growth_direction(recursion_depth + 1); } } main () { test_stack_growth_direction(0); }
out on MAC
0x7fff6e9e19ac 0x7fff6f9e89a8 0x7fff6f9e8988 0x7fff6f9e8968 0x7fff6f9e8948 0x7fff6f9e8928 0x7fff6f9e8908 0x7fff6f9e88e8 0x7fff6f9e88c8 0x7fff6f9e88a8 0x7fff6f9e8888
ubuntu output
0x7ffffeec790c 0x7ffffeec78dc 0x7ffffeec78ac 0x7ffffeec787c 0x7ffffeec784c 0x7ffffeec781c 0x7ffffeec77ec 0x7ffffeec77bc 0x7ffffeec778c 0x7ffffeec775c 0x7ffffeec772c
The stack grows on these specific settings as memory addresses decrease. It depends on the system architecture and may have different behavior for other architectures. 0x7fff6f9e8868
source share