BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

operator ++ doubts
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Petar Popara
Guest





PostPosted: Wed Mar 09, 2005 3:50 pm    Post subject: operator ++ doubts Reply with quote




I have this case:

int i=0;
int result = (i++) + (i++);

and result is 0.

I would expect that after first "i++", "i" is incremented and result would
be 1, but it seems that "i" is incremented only after whole line of code is
executed (line: int result = (i++) + (i++)Wink?


Back to top
Wiljo
Guest





PostPosted: Wed Mar 09, 2005 4:15 pm    Post subject: Re: operator ++ doubts Reply with quote



Petar Popara wrote:

Quote:
I have this case:

int i=0;
int result = (i++) + (i++);

and result is 0.

I would expect that after first "i++", "i" is incremented and result would
be 1, but it seems that "i" is incremented only after whole line of code is
executed (line: int result = (i++) + (i++)Wink?


Hello,


This ++ operator isn't called post operator for nothing. It increments the
associated variable after the expression has been evaluated. One could use the
pre operator instead, thus ++i. The increment will then take place before the
expression is evaluated:

int i = 0;
int result = (++i) + (++i);

Here i becomes 2 and result would be 4 (2 + 2). Needless to say that writing
this kind of expressions is curious at best. Better write seperate statements,
so everyone is clear about what is happening:

int i = 0;
++i;
++i;
int result = i + i;

This takes more lines, but everyone will know exactly what is going on.

I might add that using these operators with a macro will be even worse.

Say for instance we have a MAX macro:

#define MAX( a, b ) ((a) > (b) ? (a) : (b))

Do you know the value of result after the next bit of code:

int i = 0;
int result = MAX( i++, ++i );

How many times will i be incremented here?

The line would translate to:

int result = ((i++) > (++i) ? (i++) : (++i));

The variable i will be incremented three times, as opposed to the two appearing
with the call of the MAX macro. Thus i equals to 3, and result? I don't know! My
guess is 2.

Wiljo.

Back to top
Danzer
Guest





PostPosted: Wed Mar 09, 2005 4:15 pm    Post subject: Re: operator ++ doubts Reply with quote



Petar Popara wrote:
Quote:
I have this case:

int i=0;
int result = (i++) + (i++);

and result is 0.

I would expect that after first "i++", "i" is incremented and result would
be 1, but it seems that "i" is incremented only after whole line of code is
executed (line: int result = (i++) + (i++)Wink?


Your results will vary between brands of C/C++ compilers. Rule of

thumb: do not increment/decrement a variable more that once in an
expression. To get an exact reason for this, I suggest you post your
question to comp.lang.c++.moderated. In that moderated group, you will
probably get an extensive answer to your question.


Back to top
Alan Bellingham
Guest





PostPosted: Wed Mar 09, 2005 4:24 pm    Post subject: Re: operator ++ doubts Reply with quote

"Petar Popara" <none> wrote:

Quote:
I have this case:

int i=0;
int result = (i++) + (i++);

and result is 0.

Yes, that looks likely. Not guaranteed, but likely.

Quote:
I would expect that after first "i++", "i" is incremented and result would
be 1, but it seems that "i" is incremented only after whole line of code is
executed (line: int result = (i++) + (i++)Wink?

Ah, Grasshopper, you are not yet one with C++.

What you've encountered is a case of Undefined Behaviour. In this case,
you are modifying the same value twice:

"Between two sequence points, an object is modified more than once, or
is modified and the prior value is read other than to determine the
value to be stored" (C Standard)

In this case, you apply ++ twice, and attempt to read the prior value
twice.

The brackets don't help.

Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK

Back to top
Wiljo
Guest





PostPosted: Wed Mar 09, 2005 4:41 pm    Post subject: Re: operator ++ doubts Reply with quote

Alan Bellingham wrote:

Quote:
"Petar Popara" <none> wrote:


I have this case:

int i=0;
int result = (i++) + (i++);

and result is 0.


Yes, that looks likely. Not guaranteed, but likely.

Why not guaranteed? Who is the Grasshopper now? Wink

The ++ operator is in this case a post increment, thus i will be incremented
twice after the expression is evaluated. Therefore result==0 and i==2. And this
is guaranteed, across every C compiler.
Quote:

I would expect that after first "i++", "i" is incremented and result would
be 1, but it seems that "i" is incremented only after whole line of code is
executed (line: int result = (i++) + (i++)Wink?

That is correct.

Ah, Grasshopper, you are not yet one with C++.
With this line of code, everyone is a Grasshopper. And this line is just normal

C-code. It is not yet even C++.
Quote:

What you've encountered is a case of Undefined Behaviour. In this case,
you are modifying the same value twice:

There is *no* Undefined Behaviour here, although it looks like it. This is just

the interpretation of the viewer. I can see what happens, but not everyone is
clear about this. Better avoid these constructions as much as possible.
Quote:
"Between two sequence points, an object is modified more than once, or
is modified and the prior value is read other than to determine the
value to be stored" (C Standard)

In this case, you apply ++ twice, and attempt to read the prior value
twice.
The attempt will succeed, and it is not an attempt. The prior or initial value

is 0, as result shows.
Quote:

The brackets don't help.
They will not indeed.

Alan Bellingham

Be clear on this: no insult is intended. Everyone can make mistakes.

See also my other post on this matter.

Wiljo.

Back to top
Alan Bellingham
Guest





PostPosted: Wed Mar 09, 2005 4:50 pm    Post subject: Re: operator ++ doubts Reply with quote

Wiljo <invalid (AT) invalid (DOT) address> wrote:

Quote:
int i = 0;
int result = (++i) + (++i);

Here i becomes 2 and result would be 4 (2 + 2).

_MIGHT_ be 4. But the expression is allowed to generate 4128423 if it
feels like it, since you've invoked undefined behaviour.

Highly unlikely, but the language at this point takes the point of view
that it will not define the effect of modifying the same variable twice
and also trying to read it 'between two sequence points'. In effect,
it's refusing to say that the modified value has been written out until
it gets to the terminating semi-colon, and that therefore the expression
could increment 0 twice.

Other languages make other decisions, but C (and thus C++) tries to put
as few limitations on the underlying code as possible.

Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK

Back to top
Gillmer J. Derge [TeamB]
Guest





PostPosted: Wed Mar 09, 2005 4:50 pm    Post subject: Re: operator ++ doubts Reply with quote

Wiljo wrote:
Quote:
There is *no* Undefined Behaviour here, although it looks like it. This
is just the interpretation of the viewer. I can see what happens, but
not everyone is clear about this. Better avoid these constructions as
much as possible.

Alan is right on this. See question 3.2 here for details:

http://www.faqs.org/faqs/C-faq/faq

--
Gillmer J. Derge [TeamB]

Back to top
Alan Bellingham
Guest





PostPosted: Wed Mar 09, 2005 4:55 pm    Post subject: Re: operator ++ doubts Reply with quote

Wiljo <invalid (AT) invalid (DOT) address> wrote:

Quote:
Be clear on this: no insult is intended. Everyone can make mistakes.

As indeed you have, I'm afraid.

Quote:
See also my other post on this matter.

Which is sadly also erroneous.

Go find the standard. Read it. Understand it. Go talk to the gurus.

Or check the C FAQ on this very subject:

http://www.eskimo.com/~scs/C-faq/q3.2.html

Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK

Back to top
Alan Bellingham
Guest





PostPosted: Wed Mar 09, 2005 5:00 pm    Post subject: Re: operator ++ doubts Reply with quote

"Gillmer J. Derge [TeamB]" <spam (AT) gillmerderge (DOT) com> wrote:

[snip] Alan is right [snip]

But of course. I'm always right. Even when I'm wrong <grin>

Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
Back to top
Duane Hebert
Guest





PostPosted: Wed Mar 09, 2005 5:19 pm    Post subject: Re: operator ++ doubts Reply with quote


"Wiljo" <invalid (AT) invalid (DOT) address> wrote

Quote:

Why not guaranteed? Who is the Grasshopper now? Wink
The ++ operator is in this case a post increment, thus i will be
incremented
twice after the expression is evaluated. Therefore result==0 and i==2. And
this
is guaranteed, across every C compiler.

I would expect that after first "i++", "i" is incremented and result
would
be 1, but it seems that "i" is incremented only after whole line of code
is
executed (line: int result = (i++) + (i++)Wink?

The problem is that the order of evaluation is undefined.
Stroustrup has a link at his FAQ that may interest you:
http://www.research.att.com/~bs/bs_faq2.html#evaluation-order


Back to top
Alan Bellingham
Guest





PostPosted: Wed Mar 09, 2005 5:23 pm    Post subject: Re: operator ++ doubts Reply with quote

"Duane Hebert" <spoo (AT) flarn (DOT) com> wrote:

Quote:
The problem is that the order of evaluation is undefined.
Stroustrup has a link at his FAQ that may interest you:
http://www.research.att.com/~bs/bs_faq2.html#evaluation-order

Oh, now that's unfair, claiming Bjarne Stroustrup as an authority.
What's he know about C++ ...

Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK

Back to top
Bob Gonder
Guest





PostPosted: Wed Mar 09, 2005 5:51 pm    Post subject: Re: operator ++ doubts Reply with quote

Alan Bellingham wrote:

Quote:
Wiljo <invalid (AT) invalid (DOT) address> wrote:
See also my other post on this matter.

Which is sadly also erroneous.

True.

Quote:
Go find the standard. Read it. Understand it. Go talk to the gurus.

Or check the C FAQ on this very subject:

http://www.eskimo.com/~scs/C-faq/q3.2.html

Alan Bellingham

Also, if you compile the code on BC++3.1, with optimizations turned
off, you get the behaviour the OP was looking for (1).
Turn on optimization, and you get 0.

I too was mad at Borland for breaking my fine code until a year or so
later when I read the reason for it in another NG.
char*BCDpack(char*dest,char*src,int n)
{
while(n--)
*(dest++) = ((*(src++)&0xf)<<4) | (*(src++)&0xf);
return dest;
}
So elegant, and it worked until I turned on optimization.

The only timing guarantee about post++ I can remember is it will hit
sometime before the code after ";"
If you know what "sequence points" are (";" is one), that's when they
hit (some time before that, anytime before that, that's what makes it
UB).



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Mar 09, 2005 5:57 pm    Post subject: Re: operator ++ doubts Reply with quote


"Wiljo" <invalid (AT) invalid (DOT) address> wrote


Quote:
int i = 0;
int result = (++i) + (++i);

Here i becomes 2 and result would be 4 (2 + 2).

More likely, it will become 3 ((1 + 2) or (2 + 1)).


Gambit



Back to top
Duane Hebert
Guest





PostPosted: Wed Mar 09, 2005 6:05 pm    Post subject: Re: operator ++ doubts Reply with quote


"Alan Bellingham" <alanb (AT) episys (DOT) com> wrote

Quote:
"Duane Hebert" <spoo (AT) flarn (DOT) com> wrote:

The problem is that the order of evaluation is undefined.
Stroustrup has a link at his FAQ that may interest you:
http://www.research.att.com/~bs/bs_faq2.html#evaluation-order

Oh, now that's unfair, claiming Bjarne Stroustrup as an authority.
What's he know about C++ ...

C++? I thought he was an authority on Grasshoppers.


Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Wed Mar 09, 2005 6:25 pm    Post subject: Re: operator ++ doubts Reply with quote

Wiljo <invalid (AT) invalid (DOT) address> writes:

Quote:
int i = 0;
int result = (++i) + (++i);

Two modifications to data at the same address without an intervening
sequence point results in undefined behavior.

--
Chris (TeamB);

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.