"Variables" tutorial available (Windows, mingw/msvc)

A

Alf P. Steinbach

* Alf P. Steinbach:
A few days ago I posted an "Hello, world!" tutorial, discussed in <url:
http://groups.google.no/[email protected]>.

As I wrote then:

<quote>
because there seems to be a lack of post-standard _correct_
tutorials: <url: http://home.no.net/dubjai/win32cpptut/>.
</quote>

This is the follow up, part 02, discussing variables (the directory
referred above contains two documents, part 01 and part 02).

I aim at the complete newbie, but as the earlier debate showed, even for
these fundamental topics there were things to be learned also for far
more experienced C++ folks.

I hope I haven't committed too many errors of my own ( ;-) ), and look
forward to corrections -- just not "it's too long", every word counts.

I've now replaced the original with a an updated version that includes a
table of assignment operators, as suggested by Thomas Hansen.

Also, I changed the wording in the advice "Do not use the the ++ and
-- operators in expressions, except idiomatic usage" to "Do not use the
result values of the ++ and -- operators, except idiomatic usage",
because although understandable it was technically meaningless.

The section about unspecified versus undefined value is unchanged
although contested; I've posted a question about it to clc++m and await
some clarification (if possible), but basically I think it's correct as
it stands since it's simply the Holy Standard's example in disguise.
 
A

Alf P. Steinbach

* Buster:
In what sense?

That they do not affect the evaluation order of the expression the calls
appear in. The first one, going in, ensures that arguments are fully
evaluated when the function starts executing. The last one, going out,
ensures that the function result copying (if any) is complete before
e.g. a destructor is called for the function result.

Of course not.

Right... ;-)

I don't think it follows. To give you something to push against, I will
say that I am thinking of the chronological ordering of inspections,
modifications and sequence points, not some lexical ordering. Therefore,
no matter in what order the subexpressions of "x = f (x) + g (x)" are
evaluated, the eventual evaluation of "f (x)" involves two sequence
points (sorry, three: i didn't mention the one at the end of "x += 7;"
which is itself a full expression), as does the evaluation of "g (x)",
and so those six sequence points occur, (at unspecified points) in time,
during the evaluation of the full expression.

I agree that they are executed in sequence (chronological ordering) _for
the sequence they apply to_, that's the point of having them.

What you have with f(x)+g(x) is however a hierarchy, not a linear
sequence.

The sequence points for the sequence of events in calling f are executed
in sequence. Likewise those for g. But the compiler is free to reorder
the top expression as g()+f(), because there's no sequence point there.
Sorry, you've lost me. Are you saying I've confused the issues of
sequence points and unspecified order of evaluation? Because I don't
think I have. f (x) must be evaluated 'all at once', and so must g (x).

Well I haven't said, but for what I'm thinking about what you might be
thinking (ha, philosophy enters clc++!), I think you may have needlessly
_differentiated_ between the concepts of sequence points and defined
evaluation order (sequence points are there to define the order),
and based on your comment about non-interference I think there's at
least a 40% dependent chance that you might then be thinking about
parallellism of some sort, which if you are would probably (75%
dependent chance?) stem from a need to make sense of the concept of
sequence point isolated from evaluation order: "it must mean something".


[snip]
But the standard says (right out loud) that it _is_ undefined behaviour.
That's what the latter two sentence of 5/4 say. If "i = ++i + 1;"
doesn't invoke undefined behaviour in virtue of those sentences, then
what does?

Yes, that is an issue, isn't it?

I'm more and more thinking it might be a defect.

Because _apparently_ the Holy Standard is inconsistent here.

Hold on, I might have just come up with a killer point. Look again at
the first commented code example after 5.4:

i = v [i++]; // the behaviour is unspecified

(i) There is no order-of-evaluation problem here (do you agree?).

On the surface yes but I think that's most likely where the problem lies
if there is no defect in this part of the standard.

(ii) If v is an std::vector (for instance), then [] is a real function
call. If I'm right about the relevance of sequence points in
functions to the evaluation of the calling expression, then it follows
that there is no problem whatsoever, and the behaviour is defined and
the result is specified. So (if I'm right) we have to assume [] is the
builtin subscript operator: that v and i are a pointer (or an array) and
an integer (but not necessarily in that order).
Right.

(Getting a headache?)
Nope.


(iii) Making that assumption, the only remaining problem is the lack of
a sequence point between the two modifications of i. (Huh? HUH?)

Yes, and there are two things to be said about that. (i) It doesn't
support either of our views. And (ii) it indicates to me that this part
of the standard was written to use a minimum of words, and so ended up
with an overly general definition of "unspecified" (or "undefined", as
the case _might_ be ;-) ) -- the example simply reflects that.

Details of (ii): it should IMO have described the evaluation tree of an
expression and and placed sequence points _inside_ each node, start and
end of evaluation of that node.

I.e., it should IMO have explicitly discussed expression hierarchies.

(iv) Therefore either the behaviour is undefined (I maintain it is),
or the result is specified. In either case the comment is wrong. What
do you think?

I think you're onto something, but you forgot that perhaps all those
mentions of "unspecified" may be right and the single mention of
"undefined" might be wrong -- or simply not sufficiently qualified.

Note that the comment does not say "the result is unspecified", which
would be very clear.

It's clear as it is (on its own, not considering the earlier text)
because "unspecified behavior" is _defined_ by §1 of the standard, I
think it was clause 13, as applying to the result.

BTW, I hope you're enjoying our little discussion.

Yep. ;-)
 
B

Buster

Alf said:
* Buster:



That they do not affect the evaluation order of the expression the calls
appear in. The first one, going in, ensures that arguments are fully
evaluated when the function starts executing. The last one, going out,
ensures that the function result copying (if any) is complete before
e.g. a destructor is called for the function result.
Agreed.



I agree that they are executed in sequence (chronological ordering) _for
the sequence they apply to_, that's the point of having them.

What you have with f(x)+g(x) is however a hierarchy, not a linear
sequence.

The sequence points for the sequence of events in calling f are executed
in sequence. Likewise those for g. But the compiler is free to reorder
the top expression as g()+f(), because there's no sequence point there.

Well, OK. Hermeneutics isn't my strong suit. I think the standard means
what it says, and I think what you're saying about trees and hierarchies
is covered by "for each allowable ordering of the subexpressions".
Well I haven't said, but for what I'm thinking about what you might be
thinking (ha, philosophy enters clc++!), I think you may have needlessly
_differentiated_ between the concepts of sequence points and defined
evaluation order (sequence points are there to define the order),
and based on your comment about non-interference I think there's at
least a 40% dependent chance that you might then be thinking about
parallellism of some sort,

.... I'm not ...
which if you are would probably (75%
dependent chance?) stem from a need to make sense of the concept of
sequence point isolated from evaluation order: "it must mean something".

No, but I remain under the impression that the two concepts are
distinct. Not orthogonal, or unrelated, just distinct.
[snip]
But the standard says (right out loud) that it _is_ undefined behaviour.
That's what the latter two sentence of 5/4 say. If "i = ++i + 1;"
doesn't invoke undefined behaviour in virtue of those sentences, then
what does?

Yes, that is an issue, isn't it?

I'm more and more thinking it might be a defect.

Because _apparently_ the Holy Standard is inconsistent here.

I'm sticking with my story for the time being. It's kept me out of
trouble so far. (Unspecified result, undefined behaviour, they're
neither of them good news anyway.)

[...]
but you forgot that perhaps all those
mentions of "unspecified" may be right and the single mention of
"undefined" might be wrong -- or simply not sufficiently qualified.

I did. But I'm not the first. I knew I hadn't made this up myself.
In 'C++ In A Nutshell' by Ray Lischner the presentation of sequence
points describes their function as regards side-effects and adds
"Also, any expression that modifies a scalar object more than once
between sequence points, that examines the value of an object and then
modifies it, yields undefined behaviour."
It's clear as it is (on its own, not considering the earlier text)
because "unspecified behavior" is _defined_ by §1 of the standard, I
think it was clause 13, as applying to the result.

Not quite, but 1.9/2-4 could be taken to support the idea that there is
an inconsistency. Apparently the standard describes "Certain aspects and
operations of the abstract machine" as implementation-defined, "Certain
other aspects and operations" as unspecified and "Certain other
operations" as undefined. I suppose that technically "other" implies
that no operation can be both unspecified and undefined.

However 1.9/7 and especially its footnote 7 seem very explicitly to
support my view: you deal with unspecified behaviour such as order
of evaluation first, then enforce the conditions on side effects and
sequence points upon "that particular execution sequence in which the
actual code is generated". The "otherwise the behaviour is undefined"
bit in 5/4 does seem like an afterthought, but I'd be surprised if it
turned out to be a slip.
 
T

Thomas Hansen

Page 13, at the bottom where it says:
"At least they weren't considered short enough in 1970 or
thereabouts, the printer teletype era"
....

I thought the post/pre/incr/decr operators where Bjarnes invention
(post 1980) and later was adopted back to C since it was considered
such a nice thing to have...?
I might be wrong but I thought that Bjarne added them for the initial
work on the C++ language...
The way I understand your sentence about them is that they were added
in 1970 (ergo in the C language)

Anyway it's probably not importent for readers, but the language
lawyers will attack you if you don't have the history right you know...
;)

..t
 
A

Alf P. Steinbach

* Thomas Hansen:
Page 13, at the bottom where it says:
"At least they weren't considered short enough in 1970 or
thereabouts, the printer teletype era"
...

I thought the post/pre/incr/decr operators where Bjarnes invention
(post 1980) and later was adopted back to C since it was considered
such a nice thing to have...?

Nope. As I recall (and I shudder to think I could have a false memory
about this!) they were in C from the beginning. At least they're listed
& discussed in my first edition of "The C Programming Language", 1978.

I might be wrong but I thought that Bjarne added them for the initial
work on the C++ language...
Nope.


The way I understand your sentence about them is that they were added
in 1970 (ergo in the C language)

Thanks. I might have to change that wording. The 1970 version of what
later became C was a language called B, created in 1970 by Ken Thompson.
I'm not sure whether B had these operators, or whether its predecessor
BCPL had them. I sort of place "modern" C (in the sense of K&R C ;-))
at 1972. So I just wrote "1970 or thereabouts"...

Anyway it's probably not importent for readers, but the language
lawyers will attack you if you don't have the history right you know...
;)

Well...
 
J

Jerry Coffin

I thought the post/pre/incr/decr operators where Bjarnes invention
(post 1980) and later was adopted back to C since it was considered
such a nice thing to have...?

No -- they've been part of C since its _very_ beginning. They were
invented by Ken Thompson as part of B, which was C's immediate
predecessor.
 
T

Tom Widmer

Buster said:
However 1.9/7 and especially its footnote 7 seem very explicitly to
support my view: you deal with unspecified behaviour such as order
of evaluation first, then enforce the conditions on side effects and
sequence points upon "that particular execution sequence in which the
actual code is generated". The "otherwise the behaviour is undefined"
bit in 5/4 does seem like an afterthought, but I'd be surprised if it
turned out to be a slip.

The examples are in error, they have undefined, not unspecified, behaviour
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351

Tom
 
A

Alf P. Steinbach

* Alf P. Steinbach:
The section about unspecified versus undefined value is unchanged
although contested; I've posted a question about it to clc++m and await
some clarification (if possible), but basically I think it's correct as
it stands since it's simply the Holy Standard's example in disguise.

As it turned out the answer came here, not in clc++m:

This is a defect in the standard, and the consensus and proposed
resolution is that the standard's examples are incorrect and should be
changed to say "undefined behavior".

I've now replaced the version referred to above with one referring to
the proposed resolution of this defect in the standard,
<url: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351>.

Thanks to Tom Widner for providing that reference, and to all others who
have participated in the discussion both here and in clc++m.

The new version is available at the same URL as before, as Word doc,
<url: http://home.no.net/dubjai/win32cpptut/w32cpptut_01_02.doc>,

with the tutorial's main HTML version page at
<url: http://home.no.net/dubjai/win32cpptut/html/>,

as before.

Cheers,

- Alf

PS: It's amazing how many subtle issues there are even at the "Hello,
world!" stage, so I wonder if it's at all _possible_ to provide a
correct C++ tutorial (the point of a tutorial is to confront the
in-practice, not avoid and brush under the carpet the hairy & contested
issues)! But, I'll try. At least, if I can find the time.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top