1

I have a very interesting problem come up while programming in MPLAB IDE v8.92. This is just a snipit of the code but this is where the problem is occuring:

struct data
{
  INT32 value;
};

struct data array[8] = {{0},{0},{0},{0},{0},{0},{0},{0}}; 
INT32 IOC[8];
INT32 IOD[8];


for ( byte i = 0; i<8; i++)
{
#ifdef DAC
  IOC[i] = 0;
  IOD[i] = 24;
  if(array[i].value == 0) array[i].value == 1;
#endif
}

The code builds with no errors and when I step through this part of the code using a ICD 3 debug, IOC and IOD are assigned correctly but the if statement is skipped! i.e. the debugger does not even stop on it. Any ideas what might have happened?

R. Johnson
  • 102
  • 12
  • NOTE: array[x].value is a 32-bit int. – R. Johnson Oct 20 '15 at 20:00
  • 4
    The compiler might be noticing that `array[i].value` is always equal to zero at that point, skipping the `if`-statement. It's hard to say what happens without further information. The assembly generated by the compiler could help, too. Can you break this down into a minimal self-contained example? – fuz Oct 20 '15 at 20:01
  • The compiler could have deduced that all `value`s are non-zero, and optimized that statement out. Or placed it elsewhere... – vonbrand Oct 20 '15 at 20:02
  • 4
    You can test FUZxxl's idea by declaring your array volatile; then the compiler won't optimize away the accesses. – PkP Oct 20 '15 at 20:02
  • You are correct, array[i].value is always equal to zero before it reaches this statement. I'll edit the code to include some more information. – R. Johnson Oct 20 '15 at 20:09
  • @FUZxxl when I look through the disassembly listing the compiler does not generate any code for the if statement. – R. Johnson Oct 20 '15 at 20:33
  • is DAC defined? :) Must be because of defines. Try assigning a value to array from a global, then see if the `if` is run – Michael Dorgan Oct 20 '15 at 20:39
  • lol yes DAC is defined much much earlier in the code. As I stated above, I was able to step into the for loop. – R. Johnson Oct 20 '15 at 20:42
  • 1
    It may be that there is a way to turn off compiler optimizations. If you build your program with optimization disabled then you will probably see that it actually executes the needless test. In general, it's usually helpful to disable optimization for a program that you intend examine with a debugger. – John Bollinger Oct 20 '15 at 20:55
  • @JohnBollinger That's a good point, I just checked the optimization of the compiler and it is currently turned off. The problem still remains though. . . – R. Johnson Oct 20 '15 at 21:09
  • AMAZING what the tiniest typo can do. . . turns out the code was not assigning the array[i].value but comparing it. I wrote it correctly here but did not write it correclty in MPLAB. XD Thank you everyone for your help! – R. Johnson Oct 20 '15 at 21:21
  • So the compiler noticed the equality check in the body of 'if' statement and did not bother to generate code for it? – ramana_k Oct 20 '15 at 21:27

2 Answers2

2

Always compile with -Wall -Werror. Your statement would have been flagged at compile time: warning: statement with no effect [-Wunused-value]

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Ya, I just discovered how to add that to the compiler in MPLAB. XD Thanks for the suggestion! – R. Johnson Oct 22 '15 at 21:20
  • @R.Johnson It helps. About a year ago, I had [properly indented]: *if (blah); x = 5;* With indent, it was hard to see the ; on the if. Triple desk checked code in 1000 lines [even printed it out and penned it up line-by-line]. Concluded compiler bug in gcc ;-) Rebuilt with clang, which flagged it immediately because clang has -Wempty-body on by default. gcc still doesn't, even with -Wall. And even with -Wempty-body, gcc's message is cryptic and clang's is direct. – Craig Estey Oct 22 '15 at 22:26
0

Thanks to everyone who helped out. Turns out I wrote the code correctly on the forum the first time but did not correct it in my code. I edited the question to show the mistake.

  if(array[i].value == 0) array[i].value == 1;

should be:

if(array[i].value == 0) array[i].value = 1;

Guess that's what I get for staring at a screen all day XD

R. Johnson
  • 102
  • 12