[STM32H7]Question about the space occupied by variables

Hello,
I use Freertos in STM32H750, I created a variable ‘a’ in the ‘vStartTask’,The code is shown below

     void vStartTask(void *pvParameter)
     {
          int a = 0;
          for(;;)
          {
                vMyPrintf("a is %d\r\n",a);
                vTaskDelay(1000);
          }
     }

My question is :
Is the variable ‘a’ use the stack space in the startup file ‘startup_stm32h750xx.s’ ?
Or Use space in 'configTOTAL_HEAP_SIZE’?

Your int a is an automatic variable, meaning that it is placed on the stack.
Which stack?
Well, if vStartTask() is called from main(), the initial stack will be in use. You can find its address in the linker file (xxx.ld).

In some projects, the initial stack will be re-used (for other purposes) once the scheduler is running.

You can avoid that by placing static (or global) variables:

int main()
{
    static int a;
}

The above variable will be placed in the data segment.

Or Use space in 'configTOTAL_HEAP_SIZE’

No, only when you use pvPortMalloc() to allocate an int.

1 Like

I think this is a task function, so this variable is allocated on the task stack. Each task has its own stack-

  1. If you create a task using xTaskCreate, the task stack is allocated using pvPortMalloc.
  2. If you create a task using xTaskCreateStatic, you supply the task stack.

So, to conclude, this variable is always placed in the task stack which may have been allocated on the heap (i.e. in configTOTAL_HEAP_SIZE) at the time of task creation if the task was created using xTaskCreate.

1 Like

Hello aggarg,
I print the address of the variable ‘a’, it is in the range of the ‘ucHeap’ array, in the range of ‘configTOTAL HEAP SIZE’

Thanks!

@aggarg wrote:

I think this is a task function, so this variable is allocated on the task stack

I got confused by the name vStartTask(). But of course yes, you are right.

The task stacks are placed in the heap, so your observations looks correct.

1 Like

Right,

You must have created the task using the xTaskCreate() API which allocates the task stack on the heap i.e. the ucHeap array (the size of which is defined by the configTOTAL_HEAP_SIZE macro) and because this variable takes space within the task stack which lies in the ucHeap array, the address lies in its range.

If you do not want this variable to take space on the heap then you must create the task using xTaskCreateStatic(), where you can provide the memory for the stack yourself, see here for example.

2 Likes