"As-if" rule question

  • Thread starter Joona I Palaste
  • Start date
J

Joona I Palaste

Consider this C program.

#include <stdio.h>
int main(void) {
printf;("Hello world!\n");
return 0;
}

When we take into account the "as-if" rule, does this mean that the
implementation is allowed to compile this into a program which does
exactly *NOTHING* - not even anything without any effects - just the
standard start-up code and then immediately afterwards an "exit" opcode?
It doesn't even have to link the printf() implementation or store
"Hello world!\n" anywhere?

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
 
N

Nick Austin

Consider this C program.

#include <stdio.h>
int main(void) {
printf;("Hello world!\n");
return 0;
}

When we take into account the "as-if" rule, does this mean that the
implementation is allowed to compile this into a program which does
exactly *NOTHING* - not even anything without any effects - just the
standard start-up code and then immediately afterwards an "exit" opcode?

It sets the return code to 0. Not exactly "nothing" but close.

The start-up code can be optimised out but I don't know of any
systems clever enough to do so.
It doesn't even have to link the printf() implementation or store
"Hello world!\n" anywhere?

Correct.

Nick.
 
A

August Derleth

Consider this C program.

#include <stdio.h>
int main(void) {
printf;("Hello world!\n");
return 0;
}

Nothing I'd write, good thing.

(Damn top bracket's in the wrong place... ;))
When we take into account the "as-if" rule

Where is this rule written? I have a copy of the Rationale (haven't read
through it, though), K&R2, and the web FAQ, and I've never read of it.
 
J

Josh Sebastian

Where is this rule written? I have a copy of the Rationale (haven't read
through it, though), K&R2, and the web FAQ, and I've never read of it.

It is in the draft standard's index as 5.1.2.3. The most pertinent
paragraph, to my mind, is 5.1.2.3/3:

In the abstract machine, all expressions are evaluated as specified by
the semantics. An actual implementation need not evaluate part of an
expression if it can deduce that its value is not used and that no
needed side effects are produced (including any caused by calling a
function or accessing a volatile object).

Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top