Is this a bug of the compiler?

J

John

The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12

When using VC++, the output is:
a=11 a++=11 ++a=11

None of them seems reasonable.
Can anybody give a pointer on this?
 
M

marbac

John said:
The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12

When using VC++, the output is:
a=11 a++=11 ++a=11

None of them seems reasonable.
Can anybody give a pointer on this?

Wow .... thats a good question in my view.

I used cout to verify it:

#include <iostream>
using namespace std;

int main()
{
int a=10;
cout << a << ";" << a++ << ";" << ++a << endl;
return 0;
}

and got the output:
12;11;11

Seems that the stream processes the attributes backwards.
Is this thought to be so?

regards marbac
 
C

Conrad Weyns

John said:
The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}

The comma that separates the parameters does not constitute a sequence
point.
The compiler is allowed to resolve the a++ and ++a subexpresions in any
order it sees fit.

(I expect you'd want: 10, 10, 11 but you very rarely get that :)
i.m.o this must be a bug. Only ++a can actually increment a. You should
never see 12.
When using VC++, the output is:
a=11 a++=11 ++a=11

This however is normal.
Your code example has essentialy "undefined behaviour".
Regards,
Conrad Weyns.
 
R

Rob Williscroft

John wrote in in
comp.lang.c++:
The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12

When using VC++, the output is:
a=11 a++=11 ++a=11

None of them seems reasonable.
Can anybody give a pointer on this?

They are all reasonable, in C++ you may not modify the
value of a variable more than once between sequence points.

In the above 'a' is modified twice before the sequence point
at the ';', the programme exhibts Undefined Behaviour, anything
can happen.

BTW main() returns int and you're missing a ';' after the
defenition of 'a'.

Rob.
 
H

Howard

Conrad Weyns said:
The comma that separates the parameters does not constitute a sequence
point.
The compiler is allowed to resolve the a++ and ++a subexpresions in any
order it sees fit.

(I expect you'd want: 10, 10, 11 but you very rarely get that :)
i.m.o this must be a bug. Only ++a can actually increment a. You should
never see 12.

This however is normal.
Your code example has essentialy "undefined behaviour".

Which is why getting 12 is not a bug. With undefined behavior, you could
get any number!
 
D

Default User

John said:
The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12

When using VC++, the output is:
a=11 a++=11 ++a=11

None of them seems reasonable.
Can anybody give a pointer on this?

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




Brian Rodenborn
 
M

marbac

Default said:

Hi,
i read through this faq now, but still there are some questions open to me.

-what is ``sequence point'' in ANSI C's terminology?
Pardon ... but i really dont know what this is.

-what does the autor mean with: ``after'' ?
-what does the autor mean with : considered ``finished''?
-what are the: ``multiple, ambiguous side effects''?

regards and thanks
 
D

Default User

marbac said:
Hi,
i read through this faq now, but still there are some questions open to me.

-what is ``sequence point'' in ANSI C's terminology?
Pardon ... but i really dont know what this is.

The FAQ author gave this link in the answer I posted.

http://www.eskimo.com/~scs/C-faq/q3.8.html
-what does the autor mean with: ``after'' ?
-what does the autor mean with : considered ``finished''?
-what are the: ``multiple, ambiguous side effects''?

I suggest you start with a dictionary of the English language.


Brian Rodenborn
 
D

Default User

marbac said:
I will not attack this, because it has nothing to do with c++


I was serious. You asked what the author meant by a bunch of
non-technical words. What exactly about his usage gave you problems?




Brian Rodenborn
 
K

Karl Heinz Buchegger

marbac said:
Hi,
i read through this faq now, but still there are some questions open to me.

-what is ``sequence point'' in ANSI C's terminology?
Pardon ... but i really dont know what this is.

No problem. But allow me to delay the answer to this question
based on my own sequence point, the end of the posting
-what does the autor mean with: ``after'' ?

The author uses "after" because a lot of programmers think that
in
a++
after means 'immediatly after'. That's not true. It is 'sometimes after'.
-what does the autor mean with : considered ``finished''?

If you have 2 statements

i = 2 * j;
k = 2 * i;

then at the first ';', the first statement is, lously speaking, considered to
be finished. You would expect the code to process the second statement only
after the first one has finished (copletely evaluated) completely.
-what are the: ``multiple, ambiguous side effects''?

The author explained what he means by that.
Anyway: in
i = a++;
There are 2 things going on
the assignment
the increment

The assignment is said to be the 'main action'. After all the whole thing is an
assignment statement. But during evaluation of that statement a second action
is done: the increment. So while the whole thing has the main purpose of
an assignment, it's side effect is to increment a.
regards and thanks

Since your post has now triggered the sequence point, back to your first question:
Basically a sequence point is a theoretical concept. Lously speaking every ';'
as well as function calls form a sequence point. It means: during the execution
of an statement, which is done by a sequence of operations, when this sequence point
is reached all side effects (such as increments) must have happend.

Example:
i = a++ + b;

The operations that need to be done to evaluate the above statements are

get value of a
get value of b
add both values
assign to i
increment a
sequence point

The compiler can choose any order it likes as long as the result is correct.
This also includes the positioning of the increment (which is a side effect).
The compiler can do it at any time it likes. The only restriction is: It has to happen
somewhere before the sequence point. So in

i = a++ + b;
a = 5;

a valid sequence would be

1) get a
2) get b
3) increment a
4) add 1) with 2)
5) store result from 4) to i
6) store 5 to a

But it would not be valid for the compiler to do

1) get a
2) get b
3) add 1) with 2)
4) store result from 3) to i
5) store 5 to a
6) increment a

because there is a sequence point after i = a++ + b; and the compiler
is not allowed to move the increment of a after that sequence point.


Another place where a sequence point is located is: after all function
arguments are evaluated, but before the function is called.

Example:

foo( a++ );

The events that need to be done are

1) get a
2) inrement a
3) pass value from 1) to foo
4) call function foo

As said: before the actual function call takes place, there is a sequence
point. That means: the increment has to take place *before* the function
is called. It would not be valid for the compiler to implement the whole
thing as follows:

1) get a
2) pass value from 1) to foo
3) call function foo
4) increment a

That's the whole concept of sequence points: Points in the evaluation
of statements, where side effects are guaranteed to have finished. In a
different wording: The last possible point in time, where they must have
been executed already.
 
O

Old Wolf

Karl Heinz Buchegger said:
Anyway: in
i = a++;
There are 2 things going on
the assignment
the increment

The assignment is said to be the 'main action'. After all the whole thing
is an assignment statement. But during evaluation of that statement
a second action is done: the increment. So while the whole thing has the
main purpose of an assignment, it's side effect is to increment a.

The 'main purpose' of any expression, is to be evaluated. The value
of "i = a++" is the same as the value of "a".

The assignment and the increment are both side-effects, of equal
importance (you can't say one is 'main' and one isn't). There is
no reason to prefer any particular ordering in time of these two
side-effects either.
Basically a sequence point is a theoretical concept. Lously speaking
every ';' as well as function calls form a sequence point. It means:
during the execution of an statement, which is done by a sequence of
operations, when this sequence point is reached all side effects
(such as increments) must have happend.

Lousliy speaking, indeed :) The sequence point only applies to
side-effects generated by the smallest expression containing that
sequence point, eg:
i = a++ + foo();
The increment of a does not have to be completed by the time of
the sequence-points in the call to foo(), although it might have been
completed already.
Example:
i = a++ + b;

The operations that need to be done to evaluate the above statements are
[snip]

Your explanation is a bit confusing (to me, anyway).
This statement involves 4 expressions:

E1: a++
E2: b
E3: E1 + E2
E4: i = E3

E1 and E4 have side-effects, E2 and E3 don't.
The side-effects can occur at any point during (or immediately
before or immediately after) the evaluation of all of these
expressions is complete.

The rules of the language imply that E1 and E2 must be
evaluated before E3, and E3 must be evaluated before
E4. However this has no bearing on when side-effects occur.

Note - of course the "as-if" rule applies, as it does to
everything in C++, I don't think it's productive to discuss it.
 

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

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top