Unexpected answer, compiler bug?

M

Mark McIntyre

Now, you might argue that the compiler should be able to detect that p
and q both point to b, so that the call to f() should draw a diagnostic.

Though that's tricky if, as Chris notes, the function f() is in a library
that was compiled long long ago on a computer far far away -or worse yet,
is still being written by a co-worker 14,000 miles away in a different
timezone. I'm not aware of any compilers that can diagnose either the
past or the future!
In C, f() can be compiled
separately from the call to f(), and by the time you go to join
the two together (the "link phase"), the information needed is
allowed to have been, and usually has been, discarded.

.... and even if it hasn't, the runtime environment might be presented
with duff user input, eg the user was told to pick two _different_
objects but chose the same one twice. A lot of gets() type bugs come lack
of /programmer/ effort to assist users who enter "ten" instead of "10"...

Remember - if you design an idiot-proof system, the universe will design
a better class of idiot.
 
H

Harald van Dijk

That's a foolish point. If it did not allow some behaviour to be
undefined, then it would have to document all behaviour on all platforms
now and as-yet uninvented.

As the most simplistic example, it could say that everything not
explicitly defined is implementation-defined. Alternatively, it could
provide a reference implementation and require other implementations to
behave the same way, except where the behaviour is explicitly
unspecified. There are probably other possibilities I haven't thought of.
None probably would have worked well for C, but it's not nearly as absurd
as you suggest it is.
 
M

Mark McIntyre

explicitly defined is implementation-defined.

I think's playing with words. The difference between IB and UB is an
artefact of the standard. Also practically speaking, no document can
define all behaviours - it'd take an infinite amount of paper. Even the
Ada standard doesn't define what happens if I compile the code on Boxing
Day while naked and playing pinochle.
Alternatively, it could
provide a reference implementation and require other implementations to
behave the same way, except where the behaviour is explicitly
unspecified.

Yes, this approach is often used. The difficulty is that while it works
fine for stuff thats highly prescribed in the first place (say the
definition of a kilogram), it doesn't work well for general-purpose
stuff.

In the case of programming languages, it would force implementations to
support non-native features, which can be exceptionally costly in terms
of both dev effort and runtime.
it's not nearly as absurd as you suggest it is.

Consider which languages are popular, widely ported and widely used. Are
they the ones with fully prescriptive standards? Or are they the ones
that leave the implementor plenty of scope to handle some behaviours as
best suits their environment?
 
F

Flash Gordon

Mark McIntyre wrote, On 26/12/07 11:46:
On Tue, 25 Dec 2007 20:01:42 -0800, Golden California Girls wrote:


Euh? Do you guys use DC in the states? If not, it doesn't matter which is
live, they're both carrying 240/110 V. Follow 'em back to the pole where
the phases split out....

Actually that is wrong in the UK. The neutral is bonded to earth at the
sub-station, although at your house it will be a bit away from the
ground voltage. See, for example, page 19 and on of this document
http://www2.theiet.org/Publish/Wire...utumn_wiring_matters__complete_no_adverts.pdf
It is the live that varies with respect to ground at the sub-station,
and this is why it is important that the live and neutral are not
swapped in your house.

This is all OT here so if you want to continue the discussion one
sensible place would be the IET forums which will be somewhere on
http://www.theiet.org/
 
K

Keith Thompson

Mark McIntyre said:
That's a foolish point. If it did not allow some behaviour to be
undefined, then it would have to document all behaviour on all platforms
now and as-yet uninvented. Thats obviously impossible. Furthermore it
would proscribe implementations from providing extensions since if
everything is defined, nothing can be added.

No, it's not obviously impossible. A language standard could
rigorously define the behavior of an abstract machine, and require all
implementations to implement that abstract machine exactly. But such
a language wouldn't really be C, even if it superficially looked like
it. In particular, implementations for real systems that differ from
the abstract machine couldn't easily take advantage of those systems'
features.
Perhaps it could start with "this standard expects its readers to have
common sense, if you're too stoopid to wire up a 3-pin plug, then stop
now".

Perhaps that's not the best example. I don't know how to wire up a
3-pin plug -- not because I'm stoopid, just because I've never had a
need to do it.

[...]
 
K

Kenneth Brody

int a=1;
a=a++;
[...]
The key point to your answer is the line:
a=a++;

it evaluates a to 1 in right side, assigns 1 to a in left side, then
increments a used in right side.

Wrong. It's undefined behavior, pure and simple.

Now, it's likely that the compiler will treat it as:

Do the following things before the next sequence point:

Store the value 1 into a.
Increment a.

However, it's free to do it in any order:

Store, then increment.
Increment, then store.
Store and increment at the same time, causing the computer
to hard-lock, and requiring a power-cycle to unlock it.

Also, as far as the C Standard is concerned, it's also free to
do none of the above, and perhaps generate code that reformats
the hard drive, after e-mailing your boss with your resignation
letter.
Use prefix increment instead, like
this:

a = ++a;

Although your particular compiler may generate code that "works"
(ie: "does what you think you meant to say"), this too is an
example of pure undefined behavior.
Of course, it's advisable you take into account all other advises
about prototype of main() and enting output with \n. :)

As well as reading up on "undefined behavior" and "sequence points".

If you want to increment something, increment it:

a++;
or
++a;
or
a += 1;
or even
a = a + 1;

Just don't invoke UB.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Mark McIntyre

Mark McIntyre wrote, On 26/12/07 11:46:

Actually that is wrong in the UK. The neutral is bonded to earth at the
sub-station,

Correct. This is however highly misleading to know, as it would lead the
unwary to think that the neutral is at ground voltage, and consequently
electrocute themselves by grabbing it.
although at your house it will be a bit away from the
ground voltage.

You're quite right. Typically its about rms 240v away!
It is the live that varies with respect to ground at the sub-station,
and this is why it is important that the live and neutral are not
swapped in your house.

Well, yes and no. Consider that you can plug light-bulbs in either way
round, and many appliances eg razors have only two completely reversible
pins. The problem is only with /earthed/ appliances (and humans).
This is all OT here

indeed.
 
M

Mark McIntyre

No, it's not obviously impossible.

I said /all/ behaviour on /all/ platforms.
A language standard could rigorously
define the behavior of an abstract machine,

What, even the behaviour if I compile naked on a Wednesday?
Perhaps that's not the best example.

I chose it on purpose, because of hte OP's earlier analogy about
electrical standards.
I don't know how to wire up a
3-pin plug -- not because I'm stoopid, just because I've never had a
need to do it.

I suspect you could work it out pretty quickly.
 
K

Keith Thompson

Mark McIntyre said:
I said /all/ behaviour on /all/ platforms.

Yes, I know.
What, even the behaviour if I compile naked on a Wednesday?

Why not? Presumably the behavior should be exactly the same as if you
compile fully clothed on Thursday.

Consider an abstract machine with, say, fixed sizes for all the
predefined types, well-defined behavior on numeric overflow, a fixed
amount of memory with a simple linear addressing scheme, and so forth.
Each implementation must correctly emulate the abstract machine, with
no permitted variations. The language is rigorously tailored to the
abstract machine.

For a real machine that's not sufficiently similar to the abstract
machine, this might require the abstract machine to be emulated from
the ground up.

[snip]
 
M

Mark McIntyre

Yes, I know.

Then you'll understand that its impossible, since no standard can predict
the future.

Because there's an infinite set of things that might happen (assuming you
subscribe to non-theistic models of time).
Consider an abstract machine with, say, fixed sizes for all the
predefined types, well-defined behavior on numeric overflow, a fixed
amount of memory with a simple linear addressing scheme, and so forth.
Each implementation must correctly emulate the abstract machine, with no
permitted variations. The language is rigorously tailored to the
abstract machine.

And now we're discussing my /other/ reason why even a standard that
completely defines everything relevant may be such a significant issue as
to render the point moot.

Incidentally, whats your point in debating this with me? Other than to
score points / out-pedant me?
 
K

Keith Thompson

Mark McIntyre said:
Then you'll understand that its impossible, since no standard can predict
the future.

Predicting the future is not necessary.
Because there's an infinite set of things that might happen (assuming you
subscribe to non-theistic models of time).

I'm talking about a *finite* abstract machine with a fixed amount of
memory. Such a machine can only have a finite number of states, and
those states can be completely described. A machine language for a
finite machine with no asynchronous actions can be completely
deterministic; there's no reason a higher-level language couldn't do
the same thing.
And now we're discussing my /other/ reason why even a standard that
completely defines everything relevant may be such a significant issue as
to render the point moot.

Sorry, I don't understand your point here. What other reason?
Incidentally, whats your point in debating this with me? Other than to
score points / out-pedant me?

I'm not trying to score points at all; I'm merely trying to have
technical discussion.

The point is that the fact that C leaves certain things undefined is a
design choice; it's not absolutely necessary for a language definition
to leave *anything* undefined.
 
J

James Kuyper

Mark said:
Then you'll understand that its impossible, since no standard can predict
the future.


Because there's an infinite set of things that might happen (assuming you
subscribe to non-theistic models of time).

It doesn't matter. If the description of the behavior doesn't depend
upon your clothing state, then it remains applicable regardless of your
clothing state. If it doesn't depend upon time, then it remains
applicable regardless of the time. Just because there's infinite set of
possibilities to be covered doesn't mean that the description of the
behavior must be infinite too; it needn't even be very large. Defining
that H(x)=2x+3 doesn't require an infinitely long description, despite
the fact that there's an infinite number of different values that x
could have.
 
S

Stephen Sprunk

Mark McIntyre said:
Perhaps it could start with "this standard expects its readers to have
common sense, if you're too stoopid to wire up a 3-pin plug, then stop
now".

Wiring a 3-pin plug _today_ is easy, because recent standards define what
each of them should be and what color wires to connect where. In the olden
days, it was undefined which of the two original prongs was which.
[1] It is Christmas time so a perfect example. Strings of lights used
to have fuses on each lead. Why?

Because if the cable broke half way along, both halves would be carrying
current and you could get fried off each half.

Wrong. One wire would be carrying current but the other wouldn't -- but you
wouldn't know which. Even today, most two-wire plugs can be inserted either
way. If it matters to the device, one prong will be larger than the other
(a so-called polarized plug) so it can only be inserted one way and/or the
plug will have a ground pin. However, to deal with older sockets that can't
take the larger prong and/or extra pin, adapters are easily found at
hardware stores, so there's still no guarantee -- even if the user inserts
the adapter correctly, the older outlet might be wired backwards (using
today's rules; at the time it was installed, it was okay).

(Yes, the "correct" solution is to install modern outlets, but if you do
that, everything between each outlet and the transformer must be brought up
to current code and an electrician has to verify it, which is a very
expensive undertaking and would affect millions of homes. Because of that,
when new codes come out, all preexisting wiring is grandfathered in _as long
as it's not modified_.)
Euh? Do you guys use DC in the states? If not, it doesn't matter which is
live, they're both carrying 240/110 V. Follow 'em back to the pole where
the phases split out....

You misunderstand American AC wiring. In a typical residential setup, the
transformer provides three wires: two hots (each 120VAC, 180 degrees out of
phase) and one neutral (via a center tap). Neutral is _not_ ground except
at the transformer and/or fuse/breaker box; inside the house, the neutral
wire can vary quite a ways from ground and it can shock you even if you're
grounded (or start a fire).

For standard 120V circuits, the fuse/breaker will be connected to one of the
hots and to neutral, and the outlets connected to the breaker. The wiring
between the fuse/breaker and the outlet was originally not standardized,
which meant you never knew which of the two prongs was the hot one and which
was neutral. Later, the left prong (facing the socket) was defined as hot,
color coded black in the wiring, and the right was neutral, color coded
white. Also, a bit later, a pin was added below for earth ground, color
coded green (or sometimes bare).

For the 240V outlets that are used for clothes dryers, air conditioners, and
heaters, there are special breakers that connect to both hots but not
neutral. Obviously in that case it doesn't matter which side is which, but
recently they've been color coded as red and black (to make it obvious
they're not 120V circuits, which are white and black). Again, a third wire
for earth ground, color coded green, was later added for devices that needed
it.

S
 
M

Mark McIntyre

Wrong. One wire would be carrying current but the other wouldn't -- but
you wouldn't know which.

All I'll say on this is that I know from experience as well as from
training that both live and neutral lines are carrying current. The above
comment is quite correct for an isolated single circuit, but not for a
normal UK housing ring-main with multiple appliances plugged in around
it.
Even today, most two-wire plugs can be
inserted either way. If it matters to the device, one prong will be
larger than the other

This may be true in the US, but not in the UK.

You misunderstand American AC wiring.

This is highly probable. My training is with UK wiring.
For standard 120V circuits,
....
For the 240V outlets
.....

most interesting (seriously). I'll add this to my font of useful info. In
the UK you get three-phase in commericial properties (120degs apart, used
for heavy machinery) but I've never come across 2-phase 180degs.
 
M

Mark McIntyre

I'm talking about a *finite* abstract machine

Note that my original comment was to the effect that no standard can
prescribe *all* behaviours. Not that it cannot prescribe a finite set in
a pre-specified environment.

(Yes, I'm being pedantic here: I realise the OP most likely meant "define
everything relevant to the langauge and its operating envrionment". ).
Sorry, I don't understand your point here. What other reason?

The other one I gave in an earlier post. The one where I noted that if
all behaviours were completely specified there'd be no room for
extensions, and a heck of a lot of platform where it would be uneconomic
or plain impossible to implement the language.
I'm not trying to score points at all; I'm merely trying to have
technical discussion.

I asked because I couldn't see the point of your comments. Sure, for a
pre-defined set of conditions its possible to define everything. That's
tautological.
The point is that the fact that C leaves certain things undefined is a
design choice; it's not absolutely necessary for a language definition
to leave *anything* undefined.

I disagree - inasmuch as I would not want a language standard to specify
a whole host of things, such as my chances getting off with a gorgeous
redhead in Tesco on a Saturday or the score in the next England cricket
match.
 
K

Keith Thompson

Mark McIntyre said:
On Fri, 28 Dec 2007 23:28:30 -0800, Keith Thompson wrote: [...]
I asked because I couldn't see the point of your comments. Sure, for a
pre-defined set of conditions its possible to define everything. That's
tautological.

And that's what I thought we were talking about.
I disagree - inasmuch as I would not want a language standard to specify
a whole host of things, such as my chances getting off with a gorgeous
redhead in Tesco on a Saturday or the score in the next England cricket
match.

I merely meant that a language standard could completely and
rigorously define the syntax and semantics of all programs written in
the language, with no behavior *of a program* left undefined. It
didn't occur to me to consider cricket matches as part of the
discussion.

The C standard leaves the behavior *of some C programs* undefined,
because doing so allows more flexibility for implementers. For each
instance of undefined behavior, we could examine the consequences of
defining it. For example, the standard could define the consequences
of signed integer overflow, at the cost of more expensive arithmetic
even when no overflow occurs. That was a deliberate choice, not a
logical necessity.
 
C

Chris Torek

(This is all off-topic ... I was going to tack a note on to a
different reply earlier, but am doing it now here.)
[descriptions snipped]

This is highly probable. My training is with UK wiring.

They are quite different in detail, although the goals are the
same. One particularly important detail is that UK wiring was more
unspecified / ad-hoc quite a bit later than US wiring, which was
standardized to pretty much what we have now back in the 1950s or
so. (That is, the NEC acquired most of today's layout features --
which wires go where and how they are color-coded -- and most states
picked those up as state law back then. Requirements for GFCI and,
now, AFCI are much newer. What I mean is that simple things like
polarized and three-prong outlets were pretty well standardized in
the US by the early 1960s at the latest; this was not the case in
the UK.)
most interesting (seriously). I'll add this to my font of useful
info. In the UK you get three-phase in commercial properties
(120degs apart, used for heavy machinery) but I've never come across
2-phase 180degs.

We have three-phase here as well, for the same reasons. I have
no doubt this is true there too: it comes in both "delta" and "wye"
configurations. However, the "two phase" (so-called) system for
residential wiring is not actually two phases -- it's just a
220-to-240-volt single phase with a center tap. The voltage from
center to either end is thus approximately 120V (RMS of course;
about 170 volts peak). The center tap is bonded to ground ("earth"
in UK terms) at the house entry, more or less (I think Stephen
mentioned this).

The local distribution is itself three phase (in the US and the UK
and Europe at least, and probably pretty much everywhere). One
phase at a time is tapped / transformed to "house voltages" to feed
residential units (but note that larger apartment complexes usually
get all three phases, and the in-complex distribution then varies).
The US tends to run about two to five houses per phase-tap, while
Europe tends to run about four to seven houses per phase-tap.
(This is mostly because US houses tend to be profligate with energy
use, compared to the UK/Europe. This in turn is mostly because
electric power is surprisingly cheap in the US. No longer quite
so true in California though :) -- including taxes and such, I
pay about US$.08/kWh in Utah, while my average cost in Calif in
2002 was about $.16/kWh, and marginal rate nearly $.20/kWh.)

One other fun trivia item: most long distance extremely-high-voltage
transmission lines in the US send three-phase AC power. There are
several DC interties, however, for longer distances and to connect
unsynchronized grids (ERCOT in Texas). If you are driving out in
the middle of nowhere and come across a series of transmission
towers, look up at the cable arms. If they come in threes, this
is an AC intertie; if they come in twos, it is a DC intertie.
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top