Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my variable not being updated?

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

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

What is happening?

Note: this is intended as a reference question for common problems. If the generic answers here don't help you, post a question containing your complete, actual code.

like image 786
Zack Avatar asked May 20 '14 19:05

Zack


People also ask

How do you update global variables?

Use of “global†keyword to modify global variable inside a function. If your function has a local variable with same name as global variable and you want to modify the global variable inside function then use 'global' keyword before the variable name at start of function i.e.

How do you update a variable in JavaScript?

Updating Variables We can update our variables in shorter ways by using the += , *= , -= or /= operators. This way, we don't have to repeat the variable name when we assign them to something. Then we increase the value of i by 2.

How long do global variables last?

Global variables are kept in the client terminal for 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well.

Can a function update a global variable?

Functions can access global variables and modify them. Modifying global variables in a function is considered poor programming practice. It is better to send a variable in as a parameter (or have it be returned in the 'return' statement).


1 Answers

There are several reasons why a variable might not keep a value. While some are arcane and difficult to debug, some of the most common reasons include:

The variable is modified by an 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 needs to be declared volatile. Volatile lets the compiler know that the variable could be modified asynchronously and that it shouldn't used a cached copy in a 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

Overrunning an array's bounds

There are no checks in C to prevent you from going beyond the bounds of an 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 bigger than the array. In most compilers, this will result in overwriting variables declared after the array (anywhere on the page, they don't even have to be declared on the next line). This scenario can occur in many different circumstances, including multi-dimensional arrays and stack corruption.

Forgetting to dereference a pointer

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

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

Masking a variable with the same name

Reusing a variable name in an inner scope (like 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"
like image 79
Zack Avatar answered Oct 05 '22 13:10

Zack