[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

a question

c/c++ programming lover

11/13/2008 2:35:00 AM

I'm sorry.Last time i made a mistake.The modified question is :
#include<stdio.h>
int main()
{
int a=5;
printf("%d",++a*++a);
return 0;
}
the result is 49;
why?very thanks.
17 Answers

Victor Bazarov

11/13/2008 2:40:00 AM

0

c/c++ programming lover wrote:
> I'm sorry.Last time i made a mistake.The modified question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49;
> why?

Why not? The behaviour of your code is not defined. Anything is
allowed to happen.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


osmium

11/13/2008 3:07:00 AM

0

"c/c++ programming lover" wrote:

> I'm sorry.Last time i made a mistake.The modified question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49;
> why?very thanks.

C and C++ are the same in this area. The link explains why that code
results in undefined behavior.

http://bytes.com/forum/thread2...


Michael DOUBEZ

11/13/2008 8:44:00 AM

0

c/c++ programming lover a écrit :
> I'm sorry.Last time i made a mistake.The modified question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49;
> why?very thanks.

Perhaps because it full moon tonight.
Or more probably because if you had had 42 as an answer you wouldn't
have learned about "sequence point"; a sign from fate perhaps.

--
Michael

Juha Nieminen

11/13/2008 7:27:00 PM

0

c/c++ programming lover wrote:
> I'm sorry.Last time i made a mistake.The modified question is :
> #include<stdio.h>
> int main()
> {
> int a=5;
> printf("%d",++a*++a);
> return 0;
> }
> the result is 49;
> why?very thanks.

What do you expect should happen? The standard only says that the
variable is incremented before usage. It doesn't specify exactly at
which point it's incremented. The compiler is perfectly allowed to
convert the printf call above to code equivalent to:

++a; ++a; printf("%d", a*a);

The compiler is completely inside the bounds given by the standard
when doing that: It's incrementing the variable before its usage.

Of course you can't trust *all* compilers interpret the situation like
that, so the result can very well vary between compilers (yet all of
them are nevertheless standard-compliant).

Pete Becker

11/13/2008 7:31:00 PM

0

On 2008-11-13 14:27:15 -0500, Juha Nieminen <nospam@thanks.invalid> said:

> c/c++ programming lover wrote:
>> I'm sorry.Last time i made a mistake.The modified question is :
>> #include<stdio.h>
>> int main()
>> {
>> int a=5;
>> printf("%d",++a*++a);
>> return 0;
>> }
>> the result is 49;
>> why?very thanks.
>
> What do you expect should happen? The standard only says that the
> variable is incremented before usage. It doesn't specify exactly at
> which point it's incremented. The compiler is perfectly allowed to
> convert the printf call above to code equivalent to:
>
> ++a; ++a; printf("%d", a*a);
>
> The compiler is completely inside the bounds given by the standard
> when doing that: It's incrementing the variable before its usage.
>
> Of course you can't trust *all* compilers interpret the situation like
> that, so the result can very well vary between compilers (yet all of
> them are nevertheless standard-compliant).

None of this matters. The behavior is undefined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

James Kanze

11/13/2008 9:28:00 PM

0

On Nov 13, 8:27 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> c/c++ programming lover wrote:
> > I'm sorry.Last time i made a mistake.The modified question is :
> > #include<stdio.h>
> > int main()
> > {
> >    int a=5;
> >    printf("%d",++a*++a);
> >    return 0;
> > }
> > the result is 49;
> > why?very thanks.

> What do you expect should happen? The standard only says that
> the variable is incremented before usage.

No it doesn't. As Pete has already said, it's irrelevant here,
since the code has undefined behavior, but all the standard says
is that the variable will be incremented before the next
sequence point. Even if there wasn't undefined behavior.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Juha Nieminen

11/13/2008 10:27:00 PM

0

James Kanze wrote:
>> What do you expect should happen? The standard only says that
>> the variable is incremented before usage.
>
> No it doesn't. As Pete has already said, it's irrelevant here,
> since the code has undefined behavior, but all the standard says
> is that the variable will be incremented before the next
> sequence point. Even if there wasn't undefined behavior.

But (as far as I know) jumping to a function call (in this case
printf) marks a sequence point, so the variable must have been
incremented before the code jumps to the printf function.

I think it's thus pretty unambiguous that the variable a will have
been incremented twice before the printf call is executed, and thus will
have a value of 7. It's also pretty unambiguous that the value 5 cannot
be part of the multiplication because the very definition of the
prefix++ operator makes that impossible. The only thing which in
practice is not guaranteed in "++a*++a" is in which order the variable a
is incremented and its value used in with the binary operator*.

Thus, in practice, only four things can conceivably happen:

1) The first "++a" is executed first, incrementing the value of a, and
the value of that expression will be 6. After this the second "++a" is
executed and this results in 7 as the second operand, and thus the
result will be 42.

2) The reverse happens: The second "++a" is executed first, its value
taken, and then the first one. The result of the multiplication will
still be the same.

3) Both "++a" operators are executed first, after which the value of a
(ie. 7) is used as the parameters of the multiplication, which results
in 49.

4) The compiler decides that the value of the expression "++a" is 6, and
thus uses 6 as the value for both operands of the operator*. Thus the
result of the multiplication will be 36. (Although I would say that this
case is the most unlikely of the four in practice.)

I see no logical reason why any compiler would produce anything else
than something that prints 36, 42 or 49.

But of course this is just theoretizing about compiler behavior in
practice. It's not like you should ever use anything like "++a*++a".

Pete Becker

11/14/2008 2:38:00 AM

0

On 2008-11-13 17:26:47 -0500, Juha Nieminen <nospam@thanks.invalid> said:

>
> But (as far as I know) jumping to a function call (in this case
> printf) marks a sequence point, so the variable must have been
> incremented before the code jumps to the printf function.

There is a sequence point before the two modifications and there is a
sequence point after the two modifications. But there is no sequence
point between the two modificationss, so the behavior is undefined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

James Kanze

11/14/2008 9:52:00 AM

0

On Nov 13, 11:26 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> James Kanze wrote:
> >> What do you expect should happen? The standard only says
> >> that the variable is incremented before usage.

> > No it doesn't. As Pete has already said, it's irrelevant
> > here, since the code has undefined behavior, but all the
> > standard says is that the variable will be incremented
> > before the next sequence point. Even if there wasn't
> > undefined behavior.

> But (as far as I know) jumping to a function call (in this
> case printf) marks a sequence point, so the variable must have
> been incremented before the code jumps to the printf function.

Yes. Calling a function or returning from one are sequence
points.

> I think it's thus pretty unambiguous that the variable a will
> have been incremented twice before the printf call is
> executed, and thus will have a value of 7.

Not in this case, because the standard says that there is
undefined behavior.

> It's also pretty unambiguous that the value 5 cannot be part
> of the multiplication because the very definition of the
> prefix++ operator makes that impossible.

Not in this case, because the standard says that there is
undefined behavior. Also because there is no sequence point
between the incrementation and the multiplication, so the update
to the variable need not have taken place. But the undefined
behavior trumps.

> The only thing which in practice is not guaranteed in
> "++a*++a" is in which order the variable a is incremented and
> its value used in with the binary operator*.

Nothing is guaranteed in practice, because there is undefined
behavior. Just what part of undefined don't you understand?

> Thus, in practice, only four things can conceivably happen:

> 1) The first "++a" is executed first, incrementing the value of a, and
> the value of that expression will be 6. After this the second "++a" is
> executed and this results in 7 as the second operand, and thus the
> result will be 42.

> 2) The reverse happens: The second "++a" is executed first, its value
> taken, and then the first one. The result of the multiplication will
> still be the same.

> 3) Both "++a" operators are executed first, after which the value of a
> (ie. 7) is used as the parameters of the multiplication, which results

> 4) The compiler decides that the value of the expression "++a" is 6, and
> thus uses 6 as the value for both operands of the operator*. Thus the
> result of the multiplication will be 36. (Although I would say that this
> case is the most unlikely of the four in practice.)

That's the only one I've actually seen (but I've not looked
much). In C, with the Microsoft compiler.

Not actually seen by me, but I have heard it reported that on
certain machines, such expressions result in reading and writing
from the same address at the same time (on a machine with dual
port memory), which results in the memory freezing up, and the
operations never finishing. (It sounds a bit strange from what
I know of hardware, but such machines have purportedly existed.)

All of which really isn't relevant, because the expression
contains undefined behavior. Since the C90 standard, at least.
(At least two people, including myself, have argued that this
should be fixed. But there's been absolutely no support for it
in the committee.)

> I see no logical reason why any compiler would produce
> anything else than something that prints 36, 42 or 49.

What part of undefined don't you understand? There's no
requirement that undefined behavior be logical.

> But of course this is just theoretizing about compiler
> behavior in practice. It's not like you should ever use
> anything like "++a*++a".

Agreed. In practice, even if it were only unspecified, what's
the use of an expression which produces random results.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Juha Nieminen

11/14/2008 6:04:00 PM

0

James Kanze wrote:
>> The only thing which in practice is not guaranteed in
>> "++a*++a" is in which order the variable a is incremented and
>> its value used in with the binary operator*.
>
> Nothing is guaranteed in practice, because there is undefined
> behavior. Just what part of undefined don't you understand?

The part where a compiler would encounter a "++a" and do something
completely different than incrementing the value of that variable for
the simple reason that later in the same expression there's another
"++a". Why would a compiler deliberately behave differently upon a "++a"
expression if it happens to appear more than once in the same expression?

Granted, you can't guarantee *how* the compiler behaves when it
encounters a "++a" (more precisely, exactly at which point the
incrementation and using the new value happen). However, my point is
that I don't understand why a compiler would deliberately act
*differently* from that behavior if there's another "++a" in the same
expression. Why would it do that? Just to annoy? It would make the
compiler more complicated and for what reason?