Joel Fieber

Compiler not preserving local variable in while loop

Discussion created by Joel Fieber on Apr 27, 2007
Latest reply on Jul 7, 2011 by Daniel Lundin
Hi all,

I am using CodeWarrior 5.6.1 for an MCF5270 target.

While debugging an error in my application I found the problem to be a local variable -- initialized once and used inside a while loop -- was unexpectedly changed during the loop body.

The variable was assigned by the compiler to a register, and the same register is used inside a function, called within the loop body. This overwrites my local var.

I'm all for optimal register use, but if the compiler chose a register (D3 in this case), why would it not preserve (push) the reg value around a function using that register?

Is this a compiler error? Has anyone else seen this? Or, can certain coding practices fool the compiler to not saving the local variable around function calls.

Source / compiled source is available but the specific case where I have proved this error does not lend itself to a clear example posting for forums.

Thanks,
Joel.

Outcomes