Processor-Dev1l said:
Processor-Dev1l wrote: [...]
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.
and because I was
trained to code this way

.
Trained to depend on how one particular release, of one particular compiler,
using one particular set of compilation settings, happens to behave? Â I hope
you never have to patch or upgrade your compiler, or even make changes to
any of your source code, as doing so can change the behavior of your code.
I would do the test myself, but I have no access to common linux
kernel now...
Unless you are investigating what "undefined behavior" means, or have a
morbid need to inflict damage into others' computers, I see no need to do
any testing on the above code.
It "formatted your hard drive"?
Well, I don't see any way how this code can format hard drive.
Yes, there is possible memory leak, but it shouldn't affect harddisk
devices... Except one concrete case when leak would set registers and
called interrupt for work with pure data on disk...
I am sorry you haven't set up buffer overflow protection...
The reason why code work on mainframe can be that the whole USS is
just a managed subsystem of z/OS...
The DS3K (originally the DS9K, or DeathStation 9000) is a mythical
machine with a C implementation that fully conforms to the letter
of the C standard, but on which undefined behavior generally results
in the worst possible outcome. For example, evaluating ``i = i++''
might erase your hard drive, and dividing by zero might launch
an ICBM.
There's also a standing joke that one of the possible results of
undefined behavior is making demons fly out of your nose ("nasal
demons").
It's all an exaggerated attempt to help people understand what
"undefined behavior" really means.
Here's what the C standard says about it:
3.4.3
1 undefined behavior
behavior, upon use of a nonportable or erroneous program construct
or of erroneous data, for which this International Standard
imposes no requirements
2 NOTE Possible undefined behavior ranges from ignoring the
situation completely with unpredictable results, to behaving
during translation or program execution in a documented manner
characteristic of the environment (with or without the issuance
of a diagnostic message), to terminating a translation or
execution (with the issuance of a diagnostic message).
3 EXAMPLE An example of undefined behavior is the behavior on
integer overflow.
The key phrase is "imposes no requirements". If your implementation,
when your program evaluates ``i = i++'', caused time to start flowing
backwards, it might violate the laws of physics, but it wouldn't
violate the requirements of the C standard.
Formatting your hard drive is a more realistic consequence. You might
be confident that it can't possibly happen, because your program
doesn't run with enough privileges that it can even attempt to format
your hard drive. But in fact undefined behavior (particularly buffer
overflows) can have almost arbitrarily bad consequences. As I
understand it, viruses and other malware often take advantage of this
kind of thing.
Furthermore, for something like
int i = 42;
i = i++;
you might think that the only possible results are setting i to 42
or setting i to 43. But in fact an optimizing compiler is allowed
to *assume* that no undefined behavior occurs, and to rearrange
and rewrite the generated code based on that assumption. If the
assumption is violated, again, arbitrarily bad things can happen.
Your code, which exhibits undefined behavior, doesn't "work".
It might happen to behave in the way you expect, but there is
simply no basis in the C standard for any expectation of how it
should behave. The answer is not to figure out what the code
does; the answer is to fix the code so its behavior is defined
(and, for bonus points, correct).