cancel
Showing results for 
Search instead for 
Did you mean: 

Can anyone explain me what is "Linker Setting" in Cube IDE?What will happen if I change the minimum Heap size and Stack size? The linker setting can be found in Project manager while initialising the project.

MGogr.1
Associate III

I tried the changing them, but could not find any difference. If I change that setting will it affect my project in way?

6 REPLIES 6
DWeb_2
Associate III

The "bare metal" heap will be used for example by calling standard functions like malloc() or free(). Depending on your ST firmware package, some string functions will also utilize the heap (I think for float prints) as well as some middleware (USB Deveice,...). In theory you could avoid using the memory which is allocated in the linker per default for the heap.

It's different for the stack: Data will constantly be pushed and popped onto the stack, for entering and exiting subroutines, saving temporary data etc. You can't get around using it.

The reason why you did not notice anything might be, that your project/compiled binary is most likely rather small. The stack is placed by the linker at the end of the RAM, and the stack grows downwards (from larger addresses to smaller ones). The rest of the required RAM data (including the heap section) is placed at the start (smallest address to largest one). If there is any overlap of these sections during the runtime, you will experience undefined behavior and errors.

TDK
Guru

The heap/stack sizes listed in the linker are used as error checks, not really as allocations. It verifies that at least that much space is available for each. You can set them both to 0 and produce a perfectly valid program as long as there is space.

There are no doubt countless resources online which discuss what the stack and heap are. Here is one:

https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

If you feel a post has answered your question, please click "Accept as Solution".

I try to increase the minimum stack and heap size then build the project again, I saw the used ram size increased but when I reset the minimum stack and heap size then build again, the used ram size is not changed.

If it means that we can set the minimum stack and heap size as high as we want? Then what is the purpose of this setting? If there is no enough ram for the program I can know it after I rebuild if I set it high enough, but if I didn't set it high enough then I won't know it and I can only get an hardfault error when I run the program......

Pavel A.
Evangelist III

If it means that we can set the minimum stack and heap size as high as we want? 

Of course not. The linker memory section within which the stack and heap are defined is placed in some physical memory area (by the "> RAM" syntax) so it must fit in that memory area.

> I didn't set it high enough then I won't know it and I can only get an hardfault error when I run the program

Do you mean that the program can run out of the available stack limits? Yes it is possible and is a real problem. Some tricks exist to cope with this, for example filling the stack area with a known pattern and check that bottom of stack has not been overwritten. More advanced MCUs have special registers to define the stack limit. And so on.

 

What I mean by "If it means that we can set the minimum stack and heap size as high as we want?" is because even I set the minimum heap and stack size to 0xFFF in the IOC setting, after I build the project, the actual minimum heap and stack size which set in the FLASH.Id file won't be changed if it is already set to a large enough size for the program since last build.

If the program used heap size exceed the actual size then when I build it, I can get an error. But if initially, I use the original setting(0x200,0x400) to build the project. The size set in the FLASH.Id won't be changed, but there won't be any error(like heap size not large enough) until I run the program. So I want to know what is the purpose of having such a setting here.

Pavel A.
Evangelist III

The purpose is to verify that specified stack+heap size fits in the physical memory area, as I wrote above. If this does not behave as you expect... well, there may be something wrong in the .ld file or stack/heap related code.