program with std::cout<<

S

Sprite

I write the following code

int x1 = 5;
std::cout<<x1--<<" "<<x1--<<" "<<x1;
and got the result
4 5 3
in VS2003
who can explain that, THX
 
K

Karl Heinz Buchegger

Sprite said:
I write the following code

int x1 = 5;
std::cout<<x1--<<" "<<x1--<<" "<<x1;
and got the result
4 5 3
in VS2003
who can explain that, THX

You encountered 'undefined behaviour'

You modify the same variable twice without an intermediate
sequence point.

Please search the groups archive at google. This point
has been beaten to death hundreds of times.
 
A

Alan

Karl Heinz Buchegger said:
You encountered 'undefined behaviour'

You modify the same variable twice without an intermediate
sequence point.

What do you mean by "without an intermediate sequence point",
please?
 
K

Karl Heinz Buchegger

Alan said:
What do you mean by "without an intermediate sequence point",
please?

As said: Please search the groups archive at google.

In short: C++ devides the execution of a program in sequences.
Each sequence is terminated with a sequence point (roughly:
every ';', every function call, ... denote a sequence point).
If you modify the same variable twice in a sequence, you have
undefined behaviour, as the compiler can do the actual assignment
anywhere within that sequence. There is only 1 restriction for the
compiler: it has to happen before the sequence point is reached. But
when exactly, is left to the compiler.
 
A

Alan

Karl Heinz Buchegger said:
As said: Please search the groups archive at google.

In short: C++ devides the execution of a program in sequences.
Each sequence is terminated with a sequence point (roughly:
every ';', every function call, ... denote a sequence point).
If you modify the same variable twice in a sequence, you have
undefined behaviour, as the compiler can do the actual assignment
anywhere within that sequence. There is only 1 restriction for the
compiler: it has to happen before the sequence point is reached. But
when exactly, is left to the compiler.

Thanks Karl.
 

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,189
Messages
2,571,015
Members
47,616
Latest member
gijoji4272

Latest Threads

Top