Stm32 Heap and Stack (Stm32 heap & Stack)

Source: Internet
Author: User

About heaps and stacks are already a menstrual topic for programmers, most of which are based on the OS layer to chat.

So, what is the distribution of stacks and stacks in a bare monolithic microcomputer? Here are the net picks:

When you just took over STM32, you wrote only one

int main ()

{

while (1);

}

Build://program size:code=340 ro-data=252 rw-data=0 zi-data=1632

After compiling, you will find that this program has used more than 1600 of RAM, if on the 51 microcontroller, the pain is dead, this 1600 more ram where to go,

Analysis of the 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,

Now it's time to understand.

Stack_size EQU 0x00000400

Heap_size EQU 0x00000200

The following refers to the online data to understand the difference between heap and stack

(1) Stack: Automatically allocated and disposed by the compiler, storing parameter values of functions, values of local variables, etc., in a similar manner

To the stack in the data structure.

(2) Heap area: typically assigned and released by programmers, if the programmer does not release, the program may end up being recycled by the operating system. Distribution

The method is similar to a linked list in a data structure.

(3) Global zone (static zone): The storage of global variables and static variables is placed in a block, initialized global variables and static

Variables in an area, uninitialized global variables, and uninitialized static variables in another contiguous area. After the program is completed, the system

Automatically released.

(4) Literal constant area: the 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 the text constant area Flash

static int c = 0; Static initialization Zone

p1= (char *) malloc (10); Heap Area

strcpy (P1, "123456"); "123456" is placed in the constant area

}

So the difference between heap and stack:

Stack space is automatically allocated/freed by the operating system, and space on the heap is allocated/released manually.

Stack space is limited and the heap is a large free storage area.

The program at compile time and function allocates the memory all is on the stack, and in the program running in the function call the parameter transfer also carries on on the stack.

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

1. Heap and stack size

Define size in Startup_stm32f2xx.s

Stack_size EQU 0x00000400

Area STACK, Noinit, READWRITE, align=3
Stack_mem SPACE Stack_size
__initial_sp

; Heap Configuration
; Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
;

Heap_size EQU 0x00000200

Area HEAP, Noinit, READWRITE, align=3
__heap_base

2. Heap and Stack locations

By using the map file,

Heap 0x200106f8 Section startup_stm32f2xx.o (heap)
Stack 0x200108f8 section 1024x768 startup_stm32f2xx.o (Stack)

__heap_base 0x200106f8 Data 0 startup_stm32f2xx.o (heap)
__heap_limit 0x200108f8 Data 0 startup_stm32f2xx.o (heap)
__INITIAL_SP 0x20010cf8 Data 0 startup_stm32f2xx.o (STACK)

Obviously CORTEX-M3 data: __INITIAL_SP is a stack pointer, which is the 0x8000000 address of Flash 4 bytes (it is automatically generated by the compiler based on the stack size)

Obviously the heap and stack are adjacent.

3. Heap and stack space allocation

Stack: extending to Low address

Heap: extending to High address

Obviously if you define the variables in turn

The memory address of the stack variable that is 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 heap variable defined later

4. Heap and Stack variables

Stack: Temporary variable, exiting the scope will automatically release

Heap: malloc variable, released through the free function

In addition: Stack overflow, compilation will not prompt, need attention

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

If the heap is used, the heap size must be set.
If it is a stack, it can be set to 0 without affecting the program running.
The IAR STM8 defines a stack, which is a pre-allocated area of a byte in the ram's tail as a stacking reserve area.
When a program static variable, a global variable, or a heap is in conflict with the reserved stack area, the compiler will make an error when connecting.
You can set the stack to 0 without affecting the run. (Debugging can be affected, debug will report a stack overflow warning).
There's really no need to do that.
The general program, (within the allowable range), sets how many stacks do not affect the actual RAM size used by the program,
(You can experiment with how much stack is set and the hex file is compiled),
The program still uses RAM in its original state, setting the stack to 0, not really reducing RAM usage.
Just cheat on the compiler and make the program look less RAM on the surface.
and set a certain size of the stack, it is not really more use of RAM, just let the compiler help you
Check to see if you can guarantee that the size of the RAM is not occupied and used as a stack.
The above is for IAR STM8 only.

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

From the Internet to take a look at the monolithic computer heap and stack is allocated in RAM, there may be internal also may be external, can read and write;

stacks : Temporary variables that store functions, that is, local variables, which can be used by other function stacks at any time when functions are returned. So the stack is a time-sharing storage area that is used in turn,

The compiler defines the stack_size, is in order to limit the function of the local data activities of the range, so that the range can run to fly, that is, stack overflow;

Stack_size does not affect the hex, but also does not affect how the hex runs, but in debug debugging will prompt the wrong. Stack overflow also has to be beyond the borders of the

Activities, as long as foreigners have no opinion, you can continue to play, there are foreigners do not let you play, you have to die, or everyone is dead (tear each other), some people write

Single-chip microcomputer code in the function defines a large array of int buf[8192], if the stack is less than 8192 will die very miserable.

Heap : A global variable is stored, which is theoretically accessible to all functions, and the global variable has an initial value, but this value is not in RAM and is

exists in hex, downloaded into flash, power up by code (compiler generated assembly code) moved past. Some people are very "overbearing", power on the occupation has been a very

Large RAM (Heap_size), as has (Malloc_init), others use only through their housekeeper to borrow (malloc), run out and have to change (free). So

Once the "overbearing" person appears is the compiler must define the Heap_size, otherwise and his housekeeper borrowed also no use.

In short : Heap and Stack there is in RAM, he two points to see the function demand, but he two of the total value can not exceed the actual RAM size of the microcontroller hardware, or can only

Play in the sea (drown) or build your own shipbuilding and play (external expansion ram).

Stm32 Heap and Stack (Stm32 heap & Stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.