Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're not writing your program instruction by instruction. You are describing its behavior to a C++ compiler, which then tries to express the same in machine code.</p> <p>The compiler is allowed to reorder your code, as long as the <em>observable</em> behavior does not change.</p> <p>In other words, the compiler is almost certainly reordering your code. So why does the observable behavior change?</p> <p>Because your code exhibits <em>undefined behavior</em>.</p> <p>Again, you are writing C++ code. C++ is a standard, a specification saying what the "meaning" of your code is. You're working under a contract that "As long as I, the programmer, write code that can be interpreted according to the C++ standard, then you, the compiler, will generate an executable whose behavior matches that of my source code".</p> <p>If your code does <em>anything</em> not specified in this standard, then you have violated this contract. You have fed the compiler code whose behavior can <em>not</em> be interpreted according to the C++ standard. And then all bets are off. The compiler trusted you. It believed that you would fulfill the contract. It analyzed your code and generated an executable based on the assumption that you would write code that had a well-defined meaning. You did not, so the compiler was working under a false assumption. And then anything it builds on top of that assumption is also invalid.</p> <p>Garbage in, garbage out. :)</p> <p>Sadly, there's no easy way to pinpoint the error. You can carefully study ever piece of your code, or you can try stepping through the offending code in the debugger. Or break into the debugger at the point where the "wrong" value is seen, and study the disassembly and how you got there.</p> <p>It's a pain, but that's undefined behavior for you. :) Static analysis tools (Valgrind on Linux, and depending on your version of Visual Studio, the /analyze switch may or may not be available. Clang has a similar option built in) may help </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload