When starting STM32 RAM space heap (Heap) and stack (Stack) distribution summarized

STM32 restart when first assigned to some of the data RAM to use global variables, and call the library occupied (is not clear what data)

And then allocated to the remaining space and Heap stack.

 

        The following is the Internet, he said on the Heap and Stack:

(1) the stack area (stack): automatically allocated by the compiler and release the stored function parameters, local variables, and the like, similar to its mode of operation

In the stack data structure.

(2) heap (heap): general distribution and released by the programmer, if the programmer does not release at the end of the program may be recovered by the operating system. distribution

Manner similar to the data structure list.

(3) global area (static area) (static): global variables and static variables are stored on a piece of initialization of global variables and static

Variables in an area, uninitialized global variables and uninitialized static variables in an area adjacent to another. After the program is released automatically by the system.

(4) Text constants District: constant string is stored here.

(5) the program code area: storing binary code function body.

        There have been doubts heap (heap) and global area is not a piece of it,
        today when the RAM address STM32 starting stack MSP and discovery (stm32 stack is growing down in the study when STM32 start, the top of the stack address is maximum size allocated a ram), as described above conclusions, the screenshot below in detail.

  

The figure is seen .map file Address Stack and Heap (spatial distribution), is seen from FIG HEAP line heap allocation starts (from 0x20000188), the STACK line (0x20003188) ends co 0x3000 bytes (12K )Space.

         Stack_Mem line (0x20003188) to stack address __initial_sp (0x20003588) a total of 0x400 bytes (1K) space. Their size is what I assigned at the beginning of the file startup_stm32f10x_md.s file size. As shown below:

 

Ah, yes the same.

Press stm32 the datasheet that its RAM from 0x20000000 to start. Here is the datasheet screenshot:

 

And my works in the heap and stack area are starting from 0x20000188, 0x20000188 then the space in front is part of what occupied it. Starting can be seen from the first shot .map file, first by the global variable occupied part 0x58 = 88Byte.

      

This part of the right size with the size of the global variable used is my project has been, as the result of RW-data size chart compiled project that is global variables used in the project size

 

As for what 0x20000188-0x20000058 = remaining space, I guess it should be occupied by the library. As a suffix libspace (.bss), see the FIG.

At this point the analysis ends
----------------
Disclaimer: This article is CSDN blogger "eat rice bowl" of the original article, follow the CC 4.0 BY-SA copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/qq_29119171/article/details/53764823

 

 

Original Address: http: //blog.csdn.net/slj_win/article/details/16906141

Article publishing is not very good, but the writing is still very reasonable.

 

About heap and stack already programmer month by topic, most have based os layer of talk.


So, heap and stack in a naked microcontroller what distribution it? The following is a network Abstract:

just took over the STM32, you just write a


  1.  
    int main ()
  2.  
    {
  3.  
    while(1);
  4.  
    }

BUILD: // Program Size: Code =  340 RO-data = 252 RW-data = 0 ZI-data = 1632


compiled, will find that such a program has been used more than 1,600 of RAM, and if the microcontroller 51, will feel bad dead, where it ran to more than 1,600 of RAM, and


analyze map, you will find the heap and stack occupied in startup_stm32f10x_md.s file (this is stm32 startup files) in its first few lines have defined above ,


which this get the point.


EQU 0x00000400 stack_size


Heap_Size EQU 0x00000200


following reference line information understand the difference between the stack and the stack


(1) stack area (stack): automatically allocated and freed by the compiler, the storage function parameters, local variables, and the like, which operate in a manner similar stack data structure.


(2) heap (heap): general distribution and released by the programmer, if the programmer does not release at the end of the program may be recovered by the operating system. Distribution similar to the data structure list.


(3) Global Area (static region) (static): global variables and static variables are stored in one and initialized global and static variables in a region, and uninitialized variables uninitialized global static variable phase an area adjacent to another. After the program is released automatically by the system.

 

(4) Text constants District: constant string is stored here.

 

(5) the program code area: storing binary code function body.



E.g:

  1. A = int 0; // global initialization region 
  2. * p1 char; // global uninitialized area
  3. main()
  4. {
  5. B int; // Stack
  6. char s[]="abc"; //栈
  7. * p3 = char "1234567"; // constant in the text area Flash
  8. static int C = 0; // static initialization region
  9. = P1 ( char *) the malloc ( 10); // heap area
  10. strcpy (p1, "123456"); // "123456" on the constant region
    }





Therefore, heap and stack difference:


the space stack is automatically assigned by the operating system / released manually space allocation / deallocation on the heap.


The limited space stack, heap is a great free store (heap Although there are a lot of store, but the store is not infinite, in stm32, the maximum heap area is determined by the SRAM area, while the SRAM area the specific size can refer to the data sheet).


Procedures are carried out on the stack at compile time and memory allocation function, and the program is running passing parameters is carried out on the stack when the function call.


-------------------------------------------------- -------------------------------------------------- -


1. heap and stack size


defines the size of the startup_stm32f2xx.s (this place should be wrong, the size is defined in startup_stm32f10x_hd.s, that is, the startup file)


stack_size is EQU 0x00000400 


the AREA sTACK, NOINIT, READWRITE, ALIGN = 3 
stack_size the sPACE Stack_Mem 
__initial_sp


; heap the Configuration 
; heap size (in Bytes) <0x0-0xFFFFFFFF:. 8> 
;


Heap_Size EQU 0x00000200 // this is allocated heap space


AREA hEAP, NOINIT, READWRITE, ALIGN  = 3
__heap_base


2. heap and stack location


through the MAP file found


HEAP 0x200106f8 startup_stm32f2xx.o Section 512 (HEAP) 
STACK 0x200108f8 Section 1024 startup_stm32f2xx.o (STACK)


__heap_base 0x200106f8 the Data 0 startup_stm32f2xx.o (HEAP) 
__heap_limit 0x200108f8 the Data 0 startup_stm32f2xx.o (HEAP ) 
__initial_sp 0x20010cf8 the data 0 startup_stm32f2xx.o (sTACK)


clearly understood Profile Cortex-m3: __ initial_sp is the stack pointer, it is the address of 0x8000000 FLASH the first 4 bytes (which according to the size of the stack, is automatically generated by the compiler)


Obviously heap and stack are adjacent.

 

 

3. heap and stack space allocated


stack: to address low expansion


heap: the high address extension


Obviously if in turn defined variables, stack memory address of the first variable defined than the stack variables defined after the memory address to define a large stack variables memory address is smaller than the memory address of the heap variables defined


4. heap and stack variables


stack: a temporary variable, exit the scope will automatically release the


heap: malloc variables, released through free function


Also: stack overflow, the compiler will not prompt need to pay attention


 


----------------------------------------------- -------------------------------------------------- -----


 


If you use HEAP, you must set HEAP size. 
If STACK, can be set to 0, it does not affect operation. 
IAR STM8 defined STACK, one byte is allocated in advance in the RAM area of the trailing end of the stack as a reserved area. 
When the program static variables, global variables, or reserved heap and stack area there is a conflict, when connected compiler will report an error. 
You can now STACK is set to 0, it does not affect the operation. (Affects debugging, the debugger will report stack overflow warning). 
In fact, no need to do so. 
General procedures (within the allowable range) setting the number of STACK, RAM size does not affect the real use of the program, 
(you can test how much the STACK set, compiled HEX file is the same), 
the program is used in accordance with its original state RAM, the STACK is set to 0, not a real reduction in RAM usage. 
It is only to deceive the compiler, so that the program looks less on the surface of the RAM. 
And set a certain size of STACK, is also not really use more of RAM, just let the compiler help you 
check whether it can ensure the size of the RAM size is not occupied, can be used as a stack. 
These are only for the STM8 IAR.


 


-------------------------------------------- -------------------------------------------------- --------


 


from stack to stack and view Internet pick microcontroller are allocated in the RAM, it is possible that there may be an internal external, you can read and write; (stm32 stack is allocated in the SRAM)


 


stack: temporary variable storage function, i.e. a local variable, it is possible at any time when the function returns the stack with other functions. Therefore, the stack is a storage area used alternately time-division,


      stack_size defined in the compiler, to limit the scope of the activities of the local data function, operation over such a scope may be running out, i.e. stack overflow;


     stack_size not affect Hex, but do not affect Hex how to run, but in the debug debugging will prompt wrong. There is a stack overflow over the national borders


     activities, as long as the foreigner had no opinion, you can then play, a foreigner will not let you play, you have to die of, or we all die (Sisha each other), and some people write


    MCU Code the definition of a large array of int buf [8192] in the function, if the stack is less than 8192 will die miserable.


 


Heap: The deposit is a global variable, this variable is theoretically can access all functions, global variables and some initial value, but this value is not present in the RAM, is


     the presence in Hex, to download the Flash, by power Code (compiler generated assembly code) conveyed past. Some people are very "high-handed", the power has to occupy a very


    Large RAM (Heap_Size), as there are already (malloc_init), they can only borrow housekeeper (malloc) with others, had run out of change (free). So  


    once the "hegemony" of the people there is that the compiler must define Heap_Size, and his housekeeper otherwise lend to no avail.


 


In short: the presence of RAM heap and stack, he has two sub function to see how much demand, but he can not exceed the total value of two actual RAM size of microcontroller hardware, or only


     into the sea to play (drowned) or build your own boat then play (extended RAM).

Guess you like

Origin www.cnblogs.com/vitaminVIP/p/11692099.html