Is i = i++; bad style?

  • Thread starter lovecreatesbea...
  • Start date
K

Keith Thompson

osmium 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?

No, you're wrong. A and B both modify i, and they do so between two
consecutive sequence points, so the behavior is undefined.

The behavior is undefined.

THE BEHAVIOR IS UNDEFINED!

Don't expect me to waste any more time answering this again.
 
F

Francine.Neary

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.

You are either as thick as a brick, or a troll. My money's on the
latter, but either option means you're best ignored.
 
R

Richard Heathfield

Keith Thompson said:
No, you're wrong. A and B both modify i, and they do so between two
consecutive sequence points, so the behavior is undefined.

The behavior is undefined.

THE BEHAVIOR IS UNDEFINED!

Don't expect me to waste any more time answering this again.


You took your time, didn't you, Keith? :)
 
D

Default User

osmium wrote:

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?

Well, if you just can't stand it, then there's always Google Groups.




Brian
 
M

Mark McIntyre

On Sat, 07 Jul 2007 13:02:43 -0700, in comp.lang.c ,

(discussing)
(and trying to define a meaning)

You're missing the point.

It is, of course, perfectly possible to define an evaluation route for
the expression above. There are several possible perfectly valid
definitions, including the one you chose.

However for the reasons Keith carefully listed, the C standardisation
committee chose not to choose a specific solution to , and instead
ruled that it was undefined.

Bear in mind that you are looking at a specific trivial example of the
general problem. Analogously, there are obvious integral solutions to
x^n+y^n=z^n for small values of n, but for larger n, the solutions
become harder to obtain.
(While there /is/ a neat proof of the general theorem, there's not
enough room in this newsgroup to explain it )

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

Can you show me how capable on learning C?

Get some good books. Attend some good classes. Write some programmes
of your own and submit them for peer review.
Don't let think you aren't capable of learning C also.

I learnt it when I was in college. I dread to think how long ago that
was now. I went back recently for a garden party, and some of the
students didn't look much older than my kids.

More terrifyingly, at least two of the staff recognised me and
exchanged pleasantries with me. Admittedly they were the head
bartender and the cellarer....
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

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.

Read this link, and then have a think about whether your posts are
sensible.

http://www.amazon.co.uk/s/ref=nb_ss...ield-keywords=c+unleashed&Go.x=0&Go.y=0&Go=Go

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
A

August Karlstrom

Keith Thompson skrev:
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.

"Actually, a language is not so much characterized by what it allows to
program, but more so by what it prevents from being expressed. Quoting
the late E. W. Dijkstra, the programmer’s most difficult, daily task is
to not mess things up. Itseems to me that the first and noble duty of a
language is to help in this eternal struggle."

From Niklaus Wirth's "Good Ideas, Through the Looking Glass",
http://www.cs.inf.ethz.ch/~wirth/Articles/GoodIdeas_origFig.pdf.


August
 
L

lovecreatesbea...

On Sat, 07 Jul 2007 13:02:43 -0700, in comp.lang.c ,


(discussing)
i = i++;

(and trying to define a meaning)

You're missing the point.

It is, of course, perfectly possible to define an evaluation route for
the expression above. There are several possible perfectly valid
definitions, including the one you chose.

After that, I recalled my memories and realized that i = i does
nothing, i = i++; consists of i = i and i++, so i = i++; just equals
to i++;

Now i = i++; is definitively correct, isn't it? Why none of those
posters or posts mentioned this. The ignorance of this point of the
machine makes me be blamed by some people including you.
 
S

santosh

After that, I recalled my memories and realized that i = i does
nothing, i = i++; consists of i = i and i++, so i = i++; just equals
to i++;

That's just one possible way the construct could actually be evaluated
by the compiler. The whole point of all the replies to you in this
thread is that this construct invokes undefined behaviour as per the
Standard - to be more precise, modifying a object more than once
without an intervening sequence point. Why the Standard chose not to
define a behaviour has also been explained to you, most notably by
Keith Thompson.
Now i = i++; is definitively correct, isn't it? Why none of those
posters or posts mentioned this.

The construct invokes undefined behaviour. That's all we are allowed
to know, and that's all we should bother to know, if we want to write
portable, correct code. Moreover, and most importantly, your construct
is just plain silly.
The ignorance of this point of the
machine makes me be blamed by some people including you.

No it's you who is ignorant. Either you're not understanding the issue
or are pretending not to.
 
C

Chris Dollin

After that, I recalled my memories and realized that i = i does
nothing, i = i++; consists of i = i and i++,

It doesn't -- not if "consists of" respects the grammar of the
language. (And if it doesn't, you have to explain why it's
relevant.)
so i = i++; just equals to i++;

No. It doesn't work like that.
Now i = i++; is definitively correct, isn't it?

No. It's definitively incorrect.
 
K

Keith Thompson

After that, I recalled my memories and realized that i = i does
nothing, i = i++; consists of i = i and i++, so i = i++; just equals
to i++;

'i = i' does not do nothing; it retrieves the value of 'i' and stores
it in 'i'. it can easily be optimized away (unless 'i' is volatile),
but optimization is irrelevant to whether the behavior is defined.
Now i = i++; is definitively correct, isn't it? Why none of those
posters or posts mentioned this. The ignorance of this point of the
machine makes me be blamed by some people including you.

You're being foolish and stubborn. It invokes undefined behavior.
 
D

Dave Vandervies

osmium said:



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.

Another possibility: My newsreader[1]'s killfile processing doesn't
actually remove killed articles, it just marks them as already-read
so I have to work harder to see them. In particular, a fresh plonk in
an active thread will probably not stop me from reading replies to the
plonkee in that thread. If there's an exchange between a plonked poster
and somebody who's actually managing to get something interesting out
of it, I'm quite likely to read the killed articles, and if they're
reasonable I may reply to them.

Occasionally I'll even decide that somebody I've plonked has corrected
their offending behavior and remove them from my killfile, though this
usually doesn't happen soon enough that people remember that I've plonked
them when they see the followups. (Though the last time that happened
it didn't take the offender long to re-offend, and I don't think that
one will be coming back out.)

(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.)

I would recommend using something fairly short (seven days?) the first
time and doubling it on every offense.


dave

[1] trn
 
M

Mark McIntyre

After that, I recalled my memories and realized that i = i does
nothing, i = i++; consists of i = i and i++, so i = i++; just equals
to i++;

Now i = i++; is definitively correct, isn't it?

No. You're STILL missing the point. Please will you actually READ the
responses you have had, and forget about any preconceptions you have.
Do NOT recall your memories of false information.
Why none of those posters or posts mentioned this.

Because it is nonsense.
The ignorance of this point of the
machine makes me be blamed by some people including you.

No. You're being blamed for not reading what people are telling you.

If you don't want to read the answers, why ask?

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
L

lovecreatesbea...

'i = i' does not do nothing; it retrieves the value of 'i' and stores
it in 'i'. it can easily be optimized away (unless 'i' is volatile),
but optimization is irrelevant to whether the behavior is defined.

Thank you very much.
And sorry to take much your time.
 
L

lovecreatesbea...

On Sun, 08 Jul 2007 08:48:53 -0700, in comp.lang.c ,

No. You're being blamed for not reading what people are telling you.

If you don't want to read the answers, why ask?

I do read every post in this thread.

Thank you.
 
L

lovecreatesbea...

LOL

At least some good has come of this thread..:)

No, I sent that post when it is about 3-4 am, and people all were
sleeping at that time. There was some problems with the different time
zone, and you get a different time display.
 
D

Daniel Rudy

At about the time of 7/5/2007 8:25 PM, (e-mail address removed)
stated the following:
i = i++;

The gcc gives out a warning on this line of code, saying "warning:
operation on `i' may be undefined".

But I think, at the end of the execution of the statement, the
variable i is eventually increased by one, am I right?

Bad style? How about results in undefined behavior. In other words, do
not do that.

Instead, use one of the following:

A) i += 1;
B) i = i + 1;
C) i++;

Preferably item C. (pun intended)


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 

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

Latest Threads

Top