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

savindra wrote on Friday, June 17, 2016:

Hey,

Task looks like this in FreeRTOS:

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

for(;:wink:
{
uint16_t usTest3;
uint16_t 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?

savindra wrote on Friday, 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?

rtel wrote on Friday, 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(;:wink: block.

savindra wrote on Friday, 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.

rtel wrote on Friday, 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.

richard_damon wrote on Friday, 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.

savindra wrote on Saturday, June 18, 2016:

Thanks for such detailed description.

I think , my doubt is clear now.