Is i = i++; bad style?

  • Thread starter lovecreatesbea...
  • Start date
P

Pietro Cerutti

Chris Dollin wrote:
[big snip]

Please, let me quote what I think is one of the best things I read on
clc so far about the results of invoking UF ...
Anything. It doesn't even have to be physically possible.

Beautiful!
 
T

Tak-Shing Chan

No. You're not right.

It's /possible/ that `i` is increased by 1. It's /possible/ that
it's decreased by 2. It's /possible/ that it's set to zero, or
bitwise-inverted, or that an exception is thrown, or that execution
is halted, or that all local variables are cleared, or that it
prints the message "The rest is silence", or that your executable
is deleted, or that your display crashes.

Anything. It's all permitted by the standard. Anything. It doesn't
even have to be physically possible.

I think the OP was asking a more subtle question, which is:
what constitutes ``upon use of'' i = i++? Does i++ occur before
the UB kicks in? This misunderstanding is more sophisticated
than usual.

Of course the standard answer is still that the standard
imposes no requirements.

Tak-Shing
 
K

Keith Thompson

I had this question when I was reading the c-faq. As for

i = i++;

I ever thought it works in following two ways.

1.
i = i /*no sequencing point followed immediately*/
i++
; /*sequencing point */

So, result: i = i + 1;

2.
i++ /*no sequencing point followed immediately,
but will the increasing effect be lost?*/
i = i /*i was assigned to itself*/
; /*sequencing point */

So, result: i = i;

And the result isn't certain.

Do I think it correctly?

Ok, I'll give this one last try.

No, you do not think it correctly. The behavior of 'i = i++;' is
undefined, because it attempts to modify the same object twice between
sequence points (C99 6.5p2). The phrase "undefined behavior" means

behavior, upon use of a nonportable or erroneous program construct
or of erroneous data, for which this International Standard
imposes no requirements

(that's C99 3.4.3); a note says

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

The standard places *no requirements* on the behavior of 'i = i ++;'.
It may increment i, it may not increment i, it may cause your computer
to burst into flames, it may make demons fly out of your nose.

You say you had a question about this while reading the FAQ. Question
3.3 says the behavior of that particular expression is undefined. It
refers to question 3.9 explains what undefined behavior means.

I am at a loss to understand why you're having difficulty with this,
especially considering how long you've been participating in this
newsgroup.

People are being rude to you because they are frustrated at your
repeated asking of questions that have already been clearly answered.
Some people are assuming that you're doing this deliberately, to be
annoying. I'm not *yet* ready to make that assumption.
 
L

lovecreatesbea...

... snip ...



'lovecreatesbeauty' just attained my PLONK list. Bye-bye.

This can be done immediately and silently, and no need to let me (or
anybody) know actually.
 
L

lovecreatesbea...

Ok, I'll give this one last try.

No, you do not think it correctly. The behavior of 'i = i++;' is
undefined, because it attempts to modify the same object twice between
sequence points (C99 6.5p2). The phrase "undefined behavior" means

The standard places *no requirements* on the behavior of 'i = i ++;'.
It may increment i, it may not increment i, it may cause your computer
to burst into flames, it may make demons fly out of your nose.

You say you had a question about this while reading the FAQ. Question
3.3 says the behavior of that particular expression is undefined. It
refers to question 3.9 explains what undefined behavior means.

I am at a loss to understand why you're having difficulty with this,
especially considering how long you've been participating in this
newsgroup.

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?
People are being rude to you because they are frustrated at your
repeated asking of questions that have already been clearly answered.
Some people are assuming that you're doing this deliberately, to be
annoying. I'm not *yet* ready to make that assumption.

Thank you first.

You just can do anything you want to do, I think.

I'm wondering on the internet asking for help from kind and
knowledgeable people. I'm not rude to people, right? I don't sit at
one's home, right?
 
K

Kenneth Brody

:
[...]
Don't you think the following program prints 11 on any or your system?
Isn't it portable?

int main(void)
{
int i;
i = 10;
i = i++;
printf("%d\n", i);
return 0;
}

On my old DS-6000, it hard-locks the computer. I understand that
the newer DS-9000 "fixes" this and gives a "bus error" instead.
(Though I may be mistaken. Perhaps someone with a spare DS-9000
and a scratch monkey can test it out?)

--
+-------------------------+--------------------+-----------------------+
| 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]>
 
K

Kenneth Brody

:
[...]
I had this question when I was reading the c-faq. As for

i = i++;

I ever thought it works in following two ways.

1.
i = i /*no sequencing point followed immediately*/
i++
; /*sequencing point */

So, result: i = i + 1;

2.
i++ /*no sequencing point followed immediately,
but will the increasing effect be lost?*/
i = i /*i was assigned to itself*/
; /*sequencing point */

So, result: i = i;

And the result isn't certain.

Do I think it correctly?

What happens on a computer where "store i in i" and "increment i"
are done in parallel?

--
+-------------------------+--------------------+-----------------------+
| 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]>
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Pietro said:
Chris Dollin wrote:
[big snip]

Please, let me quote what I think is one of the best things I read on
clc so far about the results of invoking UF ...
Anything. It doesn't even have to be physically possible.

Beautiful!

Compilers can and do reorder instructions in such a way that it is possible
undefined behaviour manifests itself as time travel -- the program crashes
before the offending code would be reached in the abstract machine. :)
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Don't you think the following program prints 11 on any or your system?
Isn't it portable?

No, it doesn't print 11 on my system, and it isn't portable. Depending on
the options of the compiler I used, I can either get an error message for
the invalid implicit declaration of printf, or output of 10. (And these are
not the only other possible behaviours, as others have very clearly stated
already.)
 
F

Flash Gordon

J

John Bode

Don't you think the following program prints 11 on any or your system?
Isn't it portable?

int main(void)
{
int i;
i = 10;
i = i++;
printf("%d\n", i);
return 0;



}

Yeah, I know I'm feeding the troll. This isn't for him.

I once explored all the permutations of i++ + i++, i++ + ++i, etc., on
both a Windows system (Visual Studio, PIII, NT 4) and a Mac (MPW, PPC,
OS 9). Each got at least two answers "wrong", and the wrong answers
differed between the two systems.

So, no, the above program is not portable, and if it manages to print
11 on any system, it's due to dumb luck, not design.
 
K

Keith Thompson

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?
Thank you first.

You just can do anything you want to do, I think.

I'm wondering on the internet asking for help from kind and
knowledgeable people. I'm not rude to people, right? I don't sit at
one's home, right?

No, but you asked a question that has been asked and answered many
many times, and that is answered quite clearly in the FAQ.
 
M

Mark McIntyre

On my old DS-6000, it hard-locks the computer. I understand that
the newer DS-9000 "fixes" this and gives a "bus error" instead.
(Though I may be mistaken. Perhaps someone with a spare DS-9000
and a scratch monkey can test it out?)

Thanks for nothing - now my monkey is suing me after my DS9K fired a
small branding iron out of the disk drive slot which burnt the words
""arg, your wife is a big hippo" on his hide. Apparently thats the new
bus error signal.
--
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

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.

Whats to explain? Its undefined - the explanation is that the C
standard says so.

Do you expect a dictionary to explain why meaningless words are
meaningless? Or an atlas to say why nonexistent places are
nonexistent? This isn't philosophy 101.

If, by chance, you meant "why does the C standard say this construct
is not defined by the C language?" then why not ask that question?

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

Why are you so impolite and unkind to people?

The above comment is neither impolite nor unkind. If, after two years
of asking the same questions, you still haven't got it, you really
aren't capable of learning C.

--
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
 
G

Gordon Burditt

Don't you think the following program prints 11 on any or your system?

I think it ought to delete it's own source code, and it could happen
in some implementation.
No.


On my old DS-6000, it hard-locks the computer. I understand that
the newer DS-9000 "fixes" this and gives a "bus error" instead.

"bus error" is shorthand for saying it blows up a busload of children.
Possibly yours.
(Though I may be mistaken. Perhaps someone with a spare DS-9000
and a scratch monkey can test it out?)

Where do I get a "scratch planet"?
 
G

Gordon Burditt

Yeah, I know I'm feeding the troll. This isn't for him.
I once explored all the permutations of i++ + i++, i++ + ++i, etc., on
both a Windows system (Visual Studio, PIII, NT 4) and a Mac (MPW, PPC,
OS 9). Each got at least two answers "wrong", and the wrong answers
differed between the two systems.

There are *NO* wrong answers here.
So, no, the above program is not portable, and if it manages to print
11 on any system, it's due to dumb luck, not design.

Or it could be malevolent design.
 
L

lovecreatesbea...

Whats to explain? Its undefined - the explanation is that the C
standard says so.

Do you expect a dictionary to explain why meaningless words are
meaningless? Or an atlas to say why nonexistent places are
nonexistent? This isn't philosophy 101.

If, by chance, you meant "why does the C standard say this construct
is not defined by the C language?" then why not ask that question?

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.
 
L

lovecreatesbea...

The above comment is neither impolite nor unkind. If, after two years
of asking the same questions, you still haven't got it, you really
aren't capable of learning C.

Can you show me how capable on learning C? Can you show me how capable
you are on programming in C? Have you done another Unix, Linux or
Windows alone? Show me some evidence? Don't let think you aren't
capable of learning C also.
 

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