variable doesn't exist in the current context in debug

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
user_3716231
Level 4
Level 4
25 sign-ins 10 sign-ins 5 sign-ins

variable doesn't exist when i debug program to this function.

thank for help

debug.PNG

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

buf[] is defined only within offset() function and never referenced.

So the compiler thought that it is not necessary and opted out.

num is constant 100, there was no reason to keep the name "num"

If you don't want to see the error about "buf" you can either

(1) set optimization mode to none in build configuration > compiler

(2) define "buf" as

volatile int16_t buf[100] ;

For the "num" I wonder if this error is a problem or not.

If there is no side effect you care about, it could be left as is.

Or I think that just like above, you can define "num" as

volatile int num = 100 ;

to avoid the error message.

moto

View solution in original post

0 Likes
3 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

buf[] is defined only within offset() function and never referenced.

So the compiler thought that it is not necessary and opted out.

num is constant 100, there was no reason to keep the name "num"

If you don't want to see the error about "buf" you can either

(1) set optimization mode to none in build configuration > compiler

(2) define "buf" as

volatile int16_t buf[100] ;

For the "num" I wonder if this error is a problem or not.

If there is no side effect you care about, it could be left as is.

Or I think that just like above, you can define "num" as

volatile int num = 100 ;

to avoid the error message.

moto

0 Likes

moto,

thanks your help.

after i set optimization mode to false in build configuration,i can see the value in the table.

but all of values in 'buf[]' array are optimized without declaring volatile.

may i ask you when to use volatile?

i only know when there is a variable updated in the isr,that one must be declared volatile.

sean

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Sean-san,

If the compiler is allowed to optimize, it tries many things to make the code faster and smaller.

Among them, if a variable is

(1) declared but never assigned

(2) assigned but never referenced

the variable is likely to be optimized.

But if you define the variable as "volatile", it is a hint for the compiler that

(1) The variable may be changed outside of the code (or code block)

    this is the case of ISR or the variable is a register affected by peripherals, etc.

(2) The variable affects peripheral(s)

   this is the case the variable is a register that affects the CPU or peripherals, etc.

   In this case, a code such as

   A = 0 ;

   A = 1 ;

   will not be optimized although it seems the first line (A = 0;) does not have meaning.

   (If A is GPIO, it make the pin to LOW then HIGH)

So in general, if you don't want to have a variable optimized,

declaring it as "volatile" is a popular method.

And/or if the variable is related to particular hardware, such as register, peripheral,

it should be declared as "volatile".

And of course. in case if the variable is to be modified by an ISR.

Well following URL should be the better explanation

volatile (computer programming) - Wikipedia

Best Regards,

22-Dec-2019

Motoo Tanaka

0 Likes