473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Is this a bug of the compiler?

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?
Jul 22 '05 #1
11 1306

"John" <jo*******@yahoo.com> wrote in message
news:9a**************************@posting.google.c om...
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 :-) ------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12
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.

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

Jul 22 '05 #2
John wrote:
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
Jul 22 '05 #3
John wrote in news:9a**************************@posting.google.c om 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

"Conrad Weyns" <we***@online.no> wrote in message
news:vz*****************@news2.e.nsc.no...

"John" <jo*******@yahoo.com> wrote in message
news:9a**************************@posting.google.c om...
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 :-)
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12


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


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

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


Jul 22 '05 #5
John wrote:

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
Jul 22 '05 #6
Default User wrote:

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


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
Jul 22 '05 #7
marbac wrote:

Default User wrote:

http://www.eskimo.com/~scs/C-faq/q3.2.html
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
Jul 22 '05 #8
Default User wrote:

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


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

regards marbac
Jul 22 '05 #9
marbac wrote:

Default User wrote:

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


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
Jul 22 '05 #10
marbac wrote:

Default User wrote:

http://www.eskimo.com/~scs/C-faq/q3.2.html
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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11
Karl Heinz Buchegger <kb******@gascad.at> wrote:

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

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.
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff Epler | last post by:
Hello. Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild...
13
by: Bryan Parkoff | last post by:
You may notice that switch (...) is much faster than function that can gain a big improved performance because it only use JMP instruction however function is required to use CALL, PUSH, and POP...
10
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
7
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I saw cuj's conformance roundup, but the result is quite old. I think many people like me want to know newer c++ standard conformance test...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
16
by: pj | last post by:
(Was originally, probably wrongly, posted to the vc subgroup.) (This doesn't appear to be a c# problem, but a problem with a bug in the Visual Studio c# compiler, but, any help will be welcome...)...
0
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around...
3
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error...
6
by: toton | last post by:
Hi, Anyone have a link to comparative study of different C++ compilers and how much they conform to C++ language standard? Most of the big platforms I know have GCC which well supports C++...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.