# What are the stack and heap set in the GCC linker script used for?

**URL:** <https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [May 10, 2025, 1:33am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088 "2025-05-10T01:33:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgarnett](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rgarnett](https://forums.freertos.org/u/rgarnett)\
**Post date:** [May 10, 2025, 1:33am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/1 "2025-05-10T01:33:02Z")

</div>

Hi

In the linker script for GCC the following are set:

```auto
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = ORIGIN(DTC_STACK) + LENGTH(DTC_STACK); /* end of "RAM" Ram type memory */

_Min_Heap_Size = 4K; /* required amount of heap */
_Min_Stack_Size = 4K; /* required amount of stack */

/* Memories definition */
MEMORY
{
  DTC_STACK (xrw) : ORIGIN = 0x10000000, LENGTH = _Min_Heap_Size + _Min_Stack_Size
  DTCM_MISC (xrw) : ORIGIN = 0x10000000 + _Min_Heap_Size + _Min_Stack_Size, LENGTH = 10K - _Min_Heap_Size + _Min_Stack_Size
  RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K - 1k
  RAM_NVS (xrw) : ORIGIN = 0x20008000, LENGTH = 1K
  FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 128K
}

```

I am guessing the stack is used for interrupt processing, saving registers etc, but what is the heap used for. What’s the best way of working out the size necessary for each of these?

Thanks  
Rob

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [May 10, 2025, 1:48am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/2 "2025-05-10T01:48:47Z")

</div>

The heap size defined in the linker file will generally be for the standard C library malloc function, which is likely not thread-safe unless you have taken the steps to make it so. (or the environment has been specially created for FreeRTOS and has taken the steps by default to make it safe).

As to figuring out how much space you need, you really need to know your application and its needs, or start big and see what is left after testing.

---

<div class="post-metadata">

**Author:** ![rgarnett](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rgarnett](https://forums.freertos.org/u/rgarnett)\
**Post date:** [May 10, 2025, 5:28am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/3 "2025-05-10T05:28:14Z")

</div>

So if I am not using dynamic memory in my startup functions and for my tasks can this be set to 0?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [May 10, 2025, 5:31am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/4 "2025-05-10T05:31:11Z")

</div>

If you are sure that you application does not use malloc (directly or indirectly), then yes, you can set it to 0.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [May 10, 2025, 12:43pm UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/5 "2025-05-10T12:43:19Z")

</div>

To be on the safe side, you may want to consult with your project map file to find out what \_Min\_Heap\_Size resolves to and what identifiers are located in that range. Some eco systems and/or HALs employ non standard and sometimes subtle deviations for their own startup code.

---

<div class="post-metadata">

**Author:** ![rgarnett](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rgarnett](https://forums.freertos.org/u/rgarnett)\
**Post date:** [May 11, 2025, 3:34am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/6 "2025-05-11T03:34:18Z")

</div>

Hi All

I did a bit of research and found that stdio - printf when used to print variables uses the heap. If printf just prints fixed text with no variables it does not use the heap. So the size of the heap must be determined by the number of variables to be printed within any printf function. So your most complex printf function determines minimum heap size if no other dynamic memory is used. If your program doesn’t use dynamic memory and you don’t printf variables the heap can be set to zero.

```auto
printf("Fred") => no heap
printf("Fred %0d, fredvar) => heap used

```

I’m glad we sorted that out!

Regards  
Rob

---

<div class="post-metadata">

**Author:** ![rgarnett](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rgarnett](https://forums.freertos.org/u/rgarnett)\
**Post date:** [May 11, 2025, 4:11am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/7 "2025-05-11T04:11:03Z")

</div>

Additional:

The stdio lib that I’m using is the stdio.h 5.3 (Berkeley) 3/15/86. I downloaded the source code at:  
[Berkey c library](https://chromium.googlesource.com/native_client/nacl-newlib/+/0670637d40139fb01b67beb7fa41846e8dd0ca27)

And sure enough malloc is used in the vFprintf.c and vfwprintf.c

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [May 11, 2025, 3:12pm UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/8 "2025-05-11T15:12:40Z")

</div>

Thank you for sharing your solution. As @richard-damon has already mentioned, if you are calling printf from multiple threads (and therefore, malloc from multiple threads) make sure either it is thread-safe or you have taken the steps required by your C library to make it thread-safe.

---

<div class="post-metadata">

**Author:** ![rgarnett](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@rgarnett](https://forums.freertos.org/u/rgarnett)\
**Post date:** [May 12, 2025, 1:43am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/9 "2025-05-12T01:43:41Z")

</div>

printf (stdio.h 5.3 (Berkeley) isn’t thread safe, but I am only using it one task at a time so I haven’t had any problems. Would a mutex be a safe way of using this printf to avoid re-entry issues? If so I would like to test this for future use.

Another option which is thread safe is:

- @author (c) Eyal Rozenberg [eyalroz1@gmx.com](mailto:eyalroz1@gmx.com)
- 

```
        2021-2022, Haifa, Palestine/Israel

```

- @author (c) Marco Paland ([info@paland.com](mailto:info@paland.com))
- 

```
        2014-2019, PALANDesign Hannover, Germany

```

The implementations are thread-safe; re-entrant; use no functions from  
the standard library; and do not dynamically allocate any memory.

printf\_Rozenberg which is thread safe and optimised for embedded systems. I have used this successfully in the past.

You can get it here:

[Fast thread safe printf](https://github.com/eyalroz/printf/graphs/contributors)

Regards  
Rob

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [May 12, 2025, 5:06am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/10 "2025-05-12T05:06:47Z")

</div>

> [@rgarnett](#):
>
> Would a mutex be a safe way of using this printf to avoid re-entry issues?

Yes, it would be.

> [@rgarnett](#):
>
> You can get it here:

Thank you for sharing!

---

<div class="post-metadata">

**Author:** ![MartinB](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@MartinB](https://forums.freertos.org/u/MartinB)\
**Post date:** [August 12, 2025, 7:59pm UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/11 "2025-08-12T19:59:45Z")

</div>

Hi

Could you (or anyone else) elaborate to what should be checked in the .map file?  
I opened up one made from Stm32Qubeide 0.19 / GCC 13.3 in amap (version 0.34) and searched for `_Min_Heap_Size` and it does not exist within the .Map file. Am I now the “safe side”?

I have already made efforts to ensure that malloc / free is not called. For instance by using `#pragma GCC poison malloc free` to make sure there are no stray calls to malloc/free

Best regards  
Martin

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 13, 2025, 6:11am UTC](https://forums.freertos.org/t/what-are-the-stack-and-heap-set-in-the-gcc-linker-script-used-for/23088/12 "2025-08-13T06:11:22Z")

</div>

> [@MartinB](#):
>
> I have already made efforts to ensure that malloc / free is not called.

If malloc/free is not called, then setting `_Min_Heap_Size` to 0 should be fine.
