-4

i made that simple code that declares 21 locals and do a ++ op so that the compiler don't show an error . but the problem is when i continue my debug my locals show that value is unavailable or error but the register window shows that my operation is done correctly .i use IAR IDE for arm , i'm using ARMv7 cortex M4 .

the code is :

int main(){
int C1=0;

.
.
.

int C21=0;

C1++;

.
.
.

C21++;


}

befor doing the ++ op

after doing the ++ op

1 Answers1

2

Local variable may be placed in a register and never hit memory. Local variables may be on the stack. Local variables may be stored somewhere else.

The C standard does not specify such things. The standard only talks about life time of variables, i.e. when it is valid to access a variable. The concept of a stack is not even part of the standard - in other words, a compliant C implementation does not need to have a stack.

The compiler is allowed to do all sorts of optimizations as long as it doesn't change the observable behavior of your program. I think that is exactly what happen in your case as this includes letting variables that are no longer used become unavailable.

Example:

int a, b;
for (a=0; a<10; ++a)
{
    // Do something not involving variable b
}
for (b=0; b<10; ++b)
{
    // Do something not involving variable a
}

In this case the compiler is allowed to use the same register for variable a and b. Variable b will be "unavailable" until the second loop starts and variable a will be "unavailable" as the second loop starts. That will be a perfect valid compiler optimization.

In general: When debuggers say something like "unavailable" or "optimized out" or similar, it means that the variable is not used anymore (i.e. not stored anywhere).

You can try to add a print of all variables just before the return. In this way the compiler is likely (but still not guaranteed) to keep the variables available so that you can see them in the debugger.

However, since your program doesn't produce any output the compiler is allowed to optimize it into a return 0;

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63