Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

scope & life of task variable outside of while loop in Task

Posted by savindra on June 17, 2016

Hey,

Task looks like this in FreeRTOS:

void TaskA(void *pvParameters) { uint16t usTest1; uint16t usTest2[256];

for(;;) { uint16t usTest3; uint16t usTest4[256];

}

}

As i understood usTest1 will be allocated RAM permanently for usTest1 and usTest2 array untill i delete the task. and usTest3 and usTest4 array will be allocated when the task will come into for loop and deallocated when task is completed.

Correct me if my understanding is right?


scope & life of task variable outside of while loop in Task

Posted by savindra on June 17, 2016

Correction:

RAM will be allocated for usTest1 and usTest2 untill ii delete the task.

RAM will be allocated for usTest3 ,usTest4 when task is started and same RAM area will be free once task is completed.

Correct me if my understanding is right?


scope & life of task variable outside of while loop in Task

Posted by rtel on June 17, 2016

From my understanding of your question - you are asking about the scope of C variables, which is a C language question rather than a FreeRTOS question. FreeRTOS is just C source code that is compiled by standard C compilers, so therefore can have no influence on the output of the compiler, so normal C scope rules apply to all the code.

In your code snipped, all the variables have block scope. usTest1 and usTest2 are valid inside the entire function block, and usTest3 and usTest4 are only valid inside the for(;;) block.


scope & life of task variable outside of while loop in Task

Posted by savindra on June 17, 2016

Yes you are right about scope of C variables.

But i wanted to ask is if i declare & define something outside of for loop in Task . Will it be reserved all the time in RAM .

Because Task starts its execution from the for loop after it is started.

If i declare & definie something outside of for loop . it will get executed only once when task is started in next cycle it will start execution from for loop only.

If this is right then FreeRTOS should store data outside of for loop in task stack. or every time task start from first instruction in task that may be outside of for loop.

I hope you understand what i am trying to say.


scope & life of task variable outside of while loop in Task

Posted by rtel on June 17, 2016

I hope you understand what i am trying to say.

Note really to be honest :o)

A task is implemented by a function. When the task starts, the task is given its own stack, and it starts executing the function at the very top of the function - just as if the function had been called by another function.

The way the C function is converted into assembly code is determined by the compiler, hopefully (?) following the C standard. To the compiler, the task is just a function like any other function, and is compiled as such. Anything you expect to happen (with respect to the point at which variables are allocated and the scope of the variables) were you to call the function will be exactly how it will work if the function is the entry point of a task - because exactly the same assembly instructions will be executing.

Therefore usTest1 and usTest2 will be allocated (on the stack or in registers, depending on the compiler and optimisation level, etc.) when the program counter executes the function entry prologue code, exactly as if the function had just been called by another function. There the variables will exist until such time that you delete the task (at which point the task's stack is also freed). That is equivalent to the variables existing until the function returned to its calling function had it just been called.

The variables inside the for() loop have block scope - the block being the brackets in the for() loop. They will therefore be allocated (on the stack or in a register) each time you enter that block, and unallocated each time you exit that block - exactly as they would do for any block scope variable in any C program.


scope & life of task variable outside of while loop in Task

Posted by richard_damon on June 17, 2016

One thing to point out, all the variables mentioned are 'auto' which means they are 'on the stack'. The stack for a task is created when you create the task so when precisely the variable get created isn't that important to the rest of the program, as the space was already reserved.


scope & life of task variable outside of while loop in Task

Posted by savindra on June 18, 2016

Thanks for such detailed description.

I think , my doubt is clear now.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner