Why is my variable not updated?

I defined a variable with an initial value.
When going through the code:

  • I see the initial value
  • My function changes value
  • When I use the variable later, it has the wrong value

What's happening?

Note. This is intended as a reference for general problems. If the general answers here do not help you, send a question containing the full, actual code .Sub>

+6
source share
2 answers

There are several reasons why a variable may not support a value. Although some of them are secret and difficult to debug, some of the most common reasons include:

Variable Modified by Interrupt

//---in main()--- unint8_t rxByte = 0; printf("%d", rxByte); //prints "0" //---later in Uart0_Rx_Handler()--- rxByte = U0RXREG; //rxByte set to (for example) 55 //---later in main()--- printf("%d", rxByte); //still prints "0"!!! 

If a variable is modified by an interrupt handler, it must be declared mutable. Volatile lets the compiler know that a variable can be changed asynchronously and that it should not use a cached copy in the register.

 //---in main()--- volatile unint8_t rxByte = 0; printf("%d", rxByte); //prints "0" //---later in Uart0_Rx_Handler()--- rxByte = U0RXREG; //rxByte set to 55 //---later in main()--- printf("%d", rxByte); //corectly prints 55 

Array restriction prevention

There are no checks in C to ensure that you don't go beyond the array.

 int array[10]; int my_var = 55; printf("%d", my_var); //prints "55" for(i=0; i<11; i++) // eleven is one too many indexes for this array { array[i] = i; } printf("%d", my_var); // prints "11"!!! 

In this case, we go through the loop 11 times, which is one index more than the array. In most compilers, this will overwrite the variables declared after the array (anywhere on the page, they do not even need to be declared on the next line). This scenario can occur in many different situations, including multidimensional arrays and stack corruption.

Forget about dereferencing a pointer

While it is trivial, forgetting the asterisk on the pointer when making assignments, the variable will not be set correctly

 int* pCount; pCount = 10; //forgot the asterisk!!! printf("%d", *pCount); //prints ?? 

Masking a variable with the same name

Reusing a variable name in the inner scope (for example, inside an if / for / while block or inside a function) hides a variable with the same name elsewhere.

 int count = 10; //count is 10 if(byteRecevied) { int count = U0RXREG; //count redeclared!!! DoSomething(count); printf("%d", count); //prints "55" } printf("%d", count); //prints "10" 
+15
source

I would like to add another possible reason for Zack's excellent answer.

Optimization

This often surprises me. A good optimizing compiler will notice when two different variables will never be used at the same time and will optimize the program by providing these variables with the same memory address. When you go through the code, you can see a variable that seems to change in the viewport. But what really happens is that the variable that shares its address is written to.

Another trick that the compiler pulls out is simply getting rid of the variable that it implements is not required. Sometimes you can do the equivalent of this in your code:

 force_a = mass_a * acceleration_a force_b = mass_b * acceleration_b total_force = force_a + force_b 

The compiler sees that there is no real need for the force_a and force_b variables, and therefore changes the code to this:

 total_force = (mass_a * acceleration_a) + (mass_b * acceleration_b) 

You will never see that force_a and force_b are updates, but you can still add them to the viewer.

When I look at my program, I am convinced that some variable or another has the wrong value in it, but when I run my program without a step, it works. Make sure this does not happen to you.

Added:

As Ashish Kulkarni mentioned, you can verify this by disabling optimization.

+7
source

Source: https://habr.com/ru/post/969605/


All Articles