Detailed analysis of the stack mechanism in STM32

When you just got STM32, you only wrote an infinite loop.

Detailed analysis of the stack mechanism in STM32

After compiling, you will find that such a program has used more than 1600 RAM. If it is on 51 MCU, it will be distressed. Where does this 1600 RAM go? Analyze the .map file and you will find it is heap and Stack occupied

In the startup_stm32f10x_md.s file, the first few lines of it have the following definitions:

Detailed analysis of the stack mechanism in STM32

Understand this, when STM32 is started, RAM is first allocated to the global variables used, as well as some data used by the calling library (not sure what the data is), and then the remaining space is allocated to Heap and Stack. . Since the memory space is allocated at startup, when there is too much demand for dynamically allocating memory, there is a problem of insufficient stack space.

Check the information on the web to understand the difference between heap and stack:

- (1) stack area (stack): automatically allocated and released by the compiler, stored parameter values ​​of functions, values ​​of local variables, etc., operating in a similar manner to the stack in the data structure.

- (2) heap: generally allocated and released by the programmer, if the programmer does not release, the program may be recycled by the operating system. The allocation is similar to a linked list in a data structure.

- (3) global area (static area) (static): global variables and static variables are stored in a block, initialized global variables and static variables in a region, uninitialized global variables and uninitialized static variables in Another adjacent area. It is automatically released by the system after the program ends.

- (4) Text constant area: The constant string is stored here.

- (5) Program code area: The binary code that stores the function body.

E.g:

Detailed analysis of the stack mechanism in STM32

So the difference between heap and stack:

- The space of the stack is automatically allocated/released by the operating system, and the space on the heap is manually allocated/released.

- The space of the stack is limited, and the heap is a large free storage area.

- The program allocates memory on the stack at compile time and function, and the parameter transfer is also performed on the stack when the function is called.

Obviously Cortex-m3 data shows that __initial_sp is the stack pointer, which is the first 4 bytes of the FLASH 0x8000000 address (it is automatically generated by the compiler according to the stack size). Obviously the heap and stack are adjacent.

Heap and stack space allocation:

- Stack: Expand to low address - Heap: Expand to high address

Obviously, if variables are defined in turn, the memory address of the stack variable defined first is larger than the memory address of the stack variable defined later, and the memory address of the first defined heap variable is smaller than the memory address of the heap variable defined later.

Heap and stack variables:

- Stack: Temporary variable, exiting the scope will be automatically released - heap: malloc variable, freed by free function

You should pay attention when writing programs:

1. So it is best not to call too deep. 2. Local variables should not be too large, such as local arrays. More than a certain number needs to be defined as a global array, because local arrays are also stored on the stack.

Lead Acid Replacement Battery

Lead Acid Replacement Battery,Lithium Iron 24V Battery,Lithium Iron 12V Battery,Lithium Ion Phosphate Battery

Shenzhen Unitronic Power System Co., Ltd , https://www.unitronicpower.com