Processor-Dev1l said:
Processor-Dev1l wrote:
On Sep 22, 10:13 am, Nick Keighley <
[email protected]>
wrote:
[...]
I just did a test on mainframe....
$ cat ctest.c
#include <stdio.h>
int main(void)
{
int x;
int y;
x = 20;
y = 35;
x = y++ + x++;
y = ++y + ++x;
printf("x = %d, y = %d\n", x, y);
return 0;}
[...]
$ ./a.out
x = 57, y = 94
$
Everything what I want is someone with linux to try it with gcc
compiler, that is
all.
why? It is Undefined Behaviour. Even changing the compiler
optimisation
level may change the result
Why? Because it works on USS cc modified compiler
"Gives the output I thought I wanted" is one of the possible outcomes of
undefined behavior.
I ran it on my DS3K, and it reformatted the hard drive. Now I have to
reinstall everything. Fortunately, the hardware itself is still intact.
...
It "formatted your hard drive"?
Well, I don't see any way how this code can format hard drive.
If you don't see how that can happen, that implies that you haven't
fully appreciated the significance of the part of the C standard which
says that when the behavior is undefined, "this International Standard
imposes no requirements". None. Nada. Zilch. Drill that into your
head. In some cases, something other than the ISO C standard may
impose relevant requirements (such as POSIX), in which case it may be
acceptable to write such code, so long as you only want to port it to
machines where that "something other" applies. However, unless
something else does impose relevant requirements, you should avoid
undefined behavior completely.
Unless, that is, you don't care what your program does. However, in
that case, why bother writing it in the first place? Not writing it,
and therefore not running it, is a lot easier, uses up less disk space
and less CPU time, and may have precisely the same effect as writing
it and executing it - "imposes no requirements" means that one of the
permitted results is that your program does absolutely nothing.
You may think "the standard may not impose any requirements, but
reality does - there's only a few realistic possibilities", but that's
an example of so-called "realism" gone soft in the brain. One of the
key purposes served by making the behavior undefined under certain
circumstances is that it allows implementions to implement various
optimizations without having to worry about the fact that those
optimizations might have unexpected consequences when applied to such
code. That means that such code has a good change of producing
arbitrarily bad unexpected consequences, as a result of such
optimizations.
Would you care to bet that you're able to anticipate every possible
way a compiler might choose to optimize your code, and thereby predict
what the consequences of those optimizations might be? Unless you have
been the sole author of a bleeding-edge aggressively optimizing C
compiler, I wouldn't recommend making such a bet, regardless of the
odds. Even if you were, the odds would have have to be pretty good to
justify such a bet.