Are NOP Expressions Needed?

I wonder if empty expressions evaluate to NOP or are compiler dependent.

// Trivial example
int main()
{
    ;;
}

      

+3


source to share


2 answers


It depends on the compiler, but the observed behavior should be that nothing happens. In practice, I'm pretty sure most compilers won't specify any code for an empty expression at all.

A conforming implementation executing a well-formed program must produce the same observable behavior as one of the possible executions of a corresponding abstract machine instance with the same program and the same input.

And the observed behavior is determined by:

The smallest requirements for a corresponding implementation:

  • Access to volatile objects is evaluated strictly according to the rules of an abstract machine.
  • At the end of the program, all data written to the files must be identical to one of the possible results of the program execution in accordance with the abstract semantics.
  • The dynamics of the input and output signals of interactive devices must occur in such a way that the request for output is actually delivered before the program waits for input. What an interactive device is is implementation-defined.

These collections are called the observed program behavior.

This is really the only requirement for implementation. This is often known as a "how-if" rule — the compiler can do whatever it likes as long as the observed behavior is as expected.



For what it's worth, these empty expressions are referred to as null statements:

An expression statement with a missing expression is called null.

If you really want NOP, you can try:

asm("nop");

      

It is, however, conditionally supported and its behavior is implementation-defined.

+6


source


or if it depends on the compiler.



It depends on the compiler ("usually"), but most sensible optimization compilers will simply ignore empty statements for efficiency, and they will usually not issue instructions NOP

.

+1


source







All Articles