Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




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 ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists