Is i = i++; bad style?

  • Thread starter lovecreatesbea...
  • Start date
R

Richard Heathfield

(e-mail address removed) said:
Can you show me how capable on learning C?

Mark McIntyre knows C far better than most people - because he takes the
time and trouble to read and understand much of what is posted in
comp.lang.c - and he need have no fear that he needs to justify his
competence in C to someone who has been failing to learn the basics for
over two years.
 
L

lovecreatesbea...

Both items just say "it's undefined", or "it's bad", or "it isn't
correct". If they are good enough faqs, they should give more rational
and deeper explanation on it. Evan Einstein has some problems with
high school mathematics assignment, right?

Question 3.9 in the FAQ says:

More significantly, once an expression or program becomes
undefined, *all* aspects of it become undefined. When an
undefined expression has (apparently) two plausible
interpretations, do not mislead yourself by imagining that the
compiler will choose one or the other. The Standard does not
require that a compiler make an obvious choice, and some compilers
don't. In this case, not only do we not know whether a or
a[i+1] is written to, it is possible that a completely unrelated
cell of the array (or any random part of memory) is written to,
and it is also not possible to predict what final value i will
receive.

That seems more than clear enough to me. And yet you misled yourself
by imagining that the compiler will choose one or the other.

There's absolutely no reason to write 'i = i++;' anyway. If you want
to increment 'i', just write 'i ++;'. That already assigns the result
to i; why do it again?


I come to you again with a different question but related to the
original one.

"why does the C standard say this construct is not defined by the C
language?"

Thank you for your time. (am i supposed to get the same answer - "it's
undefined" again :=) )
 
L

lovecreatesbea...

(e-mail address removed) said:



Mark McIntyre knows C far better than most people - because he takes the
time and trouble to read and understand much of what is posted in
comp.lang.c - and he need have no fear that he needs to justify his
competence in C to someone who has been failing to learn the basics for
over two years.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

yeah, that's so, and same as you? I ever thought he (and/or you) is
going to own microsoft, for you all are so capable of anything.

(I just wonder why your killfile doesn't work and send a unwanted
response to me directly.)
 
R

Richard Heathfield

(e-mail address removed) said:

"why does the C standard say this construct is not defined by the C
language?"

The answer has not changed since the last time this question was asked.
The Standard says:

"Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be accessed only to
determine the value to be stored."

By restricting programs in this way, the Standard gives compilers
considerable freedom in optimising code to make it run as fast as they
can manage. The price you pay for that speed is discipline.
 
R

Richard Heathfield

(e-mail address removed) said:
yeah, that's so, and same as you?

Do your research.

I ever thought he (and/or you) is
going to own microsoft, for you all are so capable of anything.

I don't *want* to own Microsoft. It would be like owning hell.
(I just wonder why your killfile doesn't work and send a unwanted
response to me directly.)

I haven't specifically killfiled *you*, although many people here have
done so. I have, however, downscored *all* gmail users, of which you
are one, except for a list of gmail users that I happen to know to be
bright (and you are not on that list). Consequently, whether I see your
article at all depends on whether I'm using a scoring filter or not,
which I sometimes do and sometimes don't. At present, the scoring
filter is off, but it's easy enough to put it on.

It's not exactly killfiling, but if I plonk someone, that's basically
what happens - I /probably/ won't get to see their articles, but I
/might/. And of course I have the same right of reply as anyone else,
even to people I have killfiled - although normally I don't bother to
exercise it because normally the reason they've been killfiled is that
they're behaving in an uncivilised way, and there's no reasoning with
such people.
 
C

Chris Dollin

Thank you. I really wanted to ask that question. I'm not a native
English speaker. The question hasn't been presented in that way,
because I don't have a good expression skill in English, and didn't
organize my words very well at that time.

It's undefined because (a) it's pointless, (b) when C was standardised
different compilers would compile it different ways, (c) it gives
manoeuvering room to code-generation that was seen to essential
for efficient code generation.

That's my reading.
 
K

Keith Thompson

Thank you. I really wanted to ask that question. I'm not a native
English speaker. The question hasn't been presented in that way,
because I don't have a good expression skill in English, and didn't
organize my words very well at that time.

Ok, so you're asking about the rationale for the rule, not necessarily
questioning the rule itself.

First off, the exact rule we're talking about (C99 6.5p2) is:

Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.

with a footnote:

This paragraph renders undefined statement expressions such as

i = ++i + 1;
a[i++] = i;

while allowing

i = i + 1;
a = i;

Sequence points occur in a number of places: between statements (you
can loosely think of a ';' as marking a sequence point), between the
operands of a ',', '&&', or '||' operator, and in a number of other
places.

The designers of the language *could* have defined strict consistent
semantics for all expressions. For example, they could have required
expressions to be evaluated in a strict left-to-right order, so that
'f1() + f2() * f3()' would always call f1, f2, and f3 in that order.
The language would have been simpler and more consistent, but
potentially less efficient.

By allowing order of evaluation to vary, the language allows compilers
to generate better and faster code. For example, rearranging
expressions can often reduce the number of pending subexpressions, and
therefore the number of registers in use, reducing the need to spill
values to memory. And there are plenty of other possible
optimizations.

The more the language specifies about expression evaluation, the more
consistent the language. The *less* the language specifies, the more
freedom the compiler has to perform optimizations. But if taken to
extremes, this could just mean getting wrong answers very very
quickly.

It turns out that, by not defining the behavior when an object is
modified twice between sequence points, the language makes it possible
to write straightforward code that does what you want while still
allowing for decent optimizations. Since undefined behavior means
that absolutely anything can happen, an optimizer is allowed to
*assume* that no undefined behavior occurs; if the assumption turns
out to be invalid, then anything it does is acceptable anyway.

Now for something like 'i = i++;', a reasonably smart compiler can
easily detect that you're invoking undefined behavior, and warn your
about it. If you consider just that example, it almost seems silly
that a compiler isn't even required to detect it as an error. But
suppose you write something like 'arr = arr[j]++;', or '*p1 =
(*p2)++;'. If i happens to equal j, or if p1 happens to equal p2,
then they also invoke UB by modifying the same object twice. But if
i!=j, or p1!=p2, then the expressions are valid -- and there may be
opportunities to evaluate subexpressions in parallel, generating code
that will work correctly if you're dealing with two distinct objects,
but can break badly if you're modifying the same object twice.

As a programmer, if you want to be sure that an expression is going to
be evaluated the way you want it to be, you can provide your own
sequence points. For example, you can assign an intermediate result
to a variable in one statements, then use that variable in another
statement; the sequence point guarantees that the value will actually
be stored.

The expressions whose behavior is undefined are often things that
would be poor style anyway, so the language's failure to define their
behavior is really no great loss. The example that started this:
i = i++;
is useful *only* as a way to illustrate the problem. If you want
to increment i, just write:
i++;

To summarize, the rules are a compromise that restricts both what the
programmer can do and what the compiler can do. The intended result
is that, with a little care, you can write code that does what you
want, and the compiler has the freedom to improve on what you wrote
without breaking your intended semantics.
 
O

osmium

Richard Heathfield said:
(e-mail address removed) said:

I haven't specifically killfiled *you*, although many people here have
done so. I have, however, downscored *all* gmail users, of which you
are one, except for a list of gmail users that I happen to know to be
bright (and you are not on that list). Consequently, whether I see your
article at all depends on whether I'm using a scoring filter or not,
which I sometimes do and sometimes don't. At present, the scoring
filter is off, but it's easy enough to put it on.

It's not exactly killfiling, but if I plonk someone, that's basically
what happens - I /probably/ won't get to see their articles, but I
/might/. And of course I have the same right of reply as anyone else,
even to people I have killfiled - although normally I don't bother to
exercise it because normally the reason they've been killfiled is that
they're behaving in an uncivilised way, and there's no reasoning with
such people.

I have the belief that the vast majority of *PLONKS* I see, not from you
especially, are virtual rather than real.
 
O

osmium

Keith Thompson said:
First off, the exact rule we're talking about (C99 6.5p2) is:

Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.

with a footnote:

This paragraph renders undefined statement expressions such as

i = ++i + 1;
a[i++] = i;

while allowing

i = i + 1;
a = i;

Sequence points occur in a number of places: between statements (you
can loosely think of a ';' as marking a sequence point), between the
operands of a ',', '&&', or '||' operator, and in a number of other
places.


<snip>

What is going on here? A real answer? After only 55 or so posts?
 
R

Richard Heathfield

osmium said:

I have the belief that the vast majority of *PLONKS* I see, not from
you especially, are virtual rather than real.

Yes, you're probably right. More so in my case, perhaps, for the reason
already explained, except that I *am* training myself - slowly - not to
reply to people for whom KNode has recorded a negative score. I am of
the opinion that a plonk should be for life, not just for Christmas.
(Unless it's a timed plonk, the "sin-bin" kind. I have to work on the
timing there. 30 days seems rather long, and 40 is interminable.)
 
R

Richard Heathfield

osmium said:

What is going on here? A real answer? After only 55 or so posts?

The real answer has been posted dozens of times in the last two years.
The OP has no grounds for complaint on that score.
 
D

Default User

osmium wrote:

I have the belief that the vast majority of PLONKS I see, not from
you especially, are virtual rather than real.

Do you have any evidence for that around here?

I guarantee that any I issue are real. I do, however, have home and
work setups. There are sometimes that I don't recall that some person
or the other plonked at one location needs to be added at the other,
although usually a reminder is issued by a further stupid posting.





Brian
 
K

Keith Thompson

osmium said:
Keith Thompson said:
First off, the exact rule we're talking about (C99 6.5p2) is:

Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.
[snip]

What is going on here? A real answer? After only 55 or so posts?

A real answer had to await a real question.
 
L

lovecreatesbea...

osmium said:
Keith Thompson said:
First off, the exact rule we're talking about (C99 6.5p2) is:
Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.
[snip]

What is going on here? A real answer? After only 55 or so posts?

A real answer had to await a real question.

Hello Keith, good night.

I come up with another thought which I had it at the beginning. I
think,

i = i++;

consists of

i++ /*A*/
i = i /*B*/

The order of the sequence of A and B isn't certain. Though operation A
and B are between the same two sequence points, I think the real one
modification is A. i = i just does nothing to the value of i. If i = i
can be omitted, i = i++; is correct (also not undefined). Do you think
so?

Think you for your time.
 
L

lovecreatesbea...

(e-mail address removed) said:







Do your research.


I don't *want* to own Microsoft. It would be like owning hell.


I haven't specifically killfiled *you*, although many people here have
done so. I have, however, downscored *all* gmail users, of which you
are one, except for a list of gmail users that I happen to know to be
bright (and you are not on that list). Consequently, whether I see your
article at all depends on whether I'm using a scoring filter or not,
which I sometimes do and sometimes don't. At present, the scoring
filter is off, but it's easy enough to put it on.

It's not exactly killfiling, but if I plonk someone, that's basically
what happens - I /probably/ won't get to see their articles, but I
/might/. And of course I have the same right of reply as anyone else,

Sir, you should know that I was not supposed to receive any insulting
message from you or anyone directly. You may be the same, right?
 
R

Richard Heathfield

Default User said:
osmium wrote:



Do you have any evidence for that around here?

I don't have stats, but FTR I agree with osmium. That is, I share his
belief that most plonks are a public expression of contempt rather than
a public record of killfiling.
I guarantee that any I issue are real. I do, however, have home and
work setups.

Quite so. See also my parallel reply, for another example of how one
might reasonably find oneself replying to someone whom one has
"killfiled".
There are sometimes that I don't recall that some person
or the other plonked at one location needs to be added at the other,
although usually a reminder is issued by a further stupid posting.

Since I "killfiled" *gmail* (and then modded up people with negative
scores whom I nevertheless knew to be clueful people), the time I've
spent on clc each day has been (a) less, (b) more pleasant, and (c)
better spent.
 
R

Richard Heathfield

(e-mail address removed) said:

Sir, you should know that I was not supposed to receive any insulting
message from you or anyone directly.

Nor have you received any insult from me.
 
O

osmium

Default User said:
osmium wrote:



Do you have any evidence for that around here?

I guarantee that any I issue are real. I do, however, have home and
work setups. There are sometimes that I don't recall that some person
or the other plonked at one location needs to be added at the other,
although usually a reminder is issued by a further stupid posting.

I am sure I have seem plonkers respond to plonkees, I have some names in
mind but I won't mention them, it would only get *me* plonked. Personally,
if I ever plonked someone my curiosity would get the best of me and I would
just *have* to see what the target said when I plonked him. What in the
world is the point of an insult if you don't have any idea what you have
accomplished?

And plonking seems to be the ultimate insult, kind of like "Your mother
wears army boots". But I never understood that, either.
 
F

Francine.Neary

I have the belief that the vast majority of *PLONKS* I see, not from you
especially, are virtual rather than real.

I suspect what often happens is that people post a *PLONK* message,
genuinely not wanting to have to read the garbage the poster comes out
with in future. But human nature makes them curious to know whether
their *PLONK* will produce a response, so they think "OK, I'll wait 24
hours to see if they come back at me, then after that I'll plonk
them". In the meantime, RL intervenes, they forget about it, calm
down, move on to other things, etc. and the plonk never actually
happens.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top