Stack settings under STM32 Keil http://blog.csdn.net/u011784994/article/details/53157614
When you just took over STM32, you only write an int main () {while (1);} Build://program size:code=340 ro-data=252 rw-data=0 zi-data=1632 compiled, you will find that this program has used more than 1600 RAM, if the 51 SCM, the pain of death, Where did the 1600-plus RAM go, analysis Map, you will find that the heap and stack occupy in the Startup_stm32f10x_md.s file, its first few lines have the above definition, this should be understood. Stack_size EQU 0x00000400 heap_size EQU 0x00000200 by the way mark, attention to this post: http://bbs.21ic.com/icview-371722-1-1.html
The following quotes online data to understand the difference between heap and stack
(1) Stack area (stack): Automatically allocated and released by the compiler, the value of the function parameters, local variables, and so on, its operation is similar to the stack in the data structure. (2) heap area (heap): Generally by programmers to assign and release, if the programmer does not release, the program at the end may be recycled by the operating system. The allocation method is similar to the linked list in the data structure. (3) Global zone (static area) (static): The storage of global and static variables is put together, initialized global and static variables in an area, uninitialized global variables and uninitialized static variables in another contiguous area. The system is automatically released after the program is finished. (4) Literal constant area: a constant string is stored here. (5) Program code area: the binary code that holds the function body.
For example: int a=0; //Global Initialization zone char *P1; //Global Uninitialized Zone main () { int B; // Stack char s[]= "abc"; //stack char *p3= "1234567"; //in literal constant area static int c = 0; //Static Initialization Area p1= (char *) malloc (10); //Heap Area strcpy (P1, "123456"); //"123456" in the constant area  &NBSP} So the difference between heap and stack: stack space is allocated/released automatically by the operating system, and the space on heap is manually allocated/released. The stack space is limited, and the heap is a large free storage area. program in the compile time and function allocation memory is on the stack, and the program in the operation of function calls in the transfer of parameters is also carried out on the stack.
Obviously cortex-m3 data know: __INITIAL_SP is the stack pointer, it is the flash 0x8000000 address the front 4 bytes (it is based on the stack size, automatically generated by the compiler)
Obviously the heap and stack are adjacent.
3. Heap and stack space allocation
Stacks: extending to low addresses
Heap: extending to High address
Obviously, if you define variables in turn
The memory address of the stack variable defined first is larger than the memory address of the stack variable defined later
The memory address of the heap variable defined first is smaller than the memory address of the later-defined heap variable
4. Heap and Stack variables
Stacks: Temporary variables, exiting the scope will be automatically released
Heap: malloc variables, which should be noted when writing a program with the free function:
1. So it's best not to call too deep.
2. Local variables should not be too large, such as local arrays, more than a certain number to be defined as global arrays, because the local array is also stored on the stack.