undefined outcome

  • Thread starter przemek drochomirecki
  • Start date
P

przemek drochomirecki

hi,

why the outcome of the following code is undefinied?

int c=0; cout<<c++<<c;


thx in adv.
przemek drochomirecki
 
S

Shezan Baig

przemek said:
hi,

why the outcome of the following code is undefinied?

int c=0; cout<<c++<<c;


The statement modifies a value and then reads it in the same statement.
That is undefined behaviour. If you need defined behaviour, split it
to 2 separate statements.

hth,
-shez-
 
V

Victor Bazarov

przemek said:
why the outcome of the following code is undefinied?

int c=0; cout<<c++<<c;

It's not undefined, it's unspecified. What you have is:

(cout.operator<<(c++)).operator<<(c);

The compiler can choose to extract the value of 'c' for the
second call before it increments 'c' as the result of ++.

V
 
V

Victor Bazarov

Shezan said:
The statement modifies a value and then reads it in the same statement.

"then"? Why "then"? Can you actually tell _when_ (in what order) it
all happens?
That is undefined behaviour.

Really? So,

a = 1, a;

has undefined behaviour? It modifies the 'a' and *then* reads it in the
same statement...

V
 
A

Alf P. Steinbach

* przemek drochomirecki:
why the outcome of the following code is undefinied?

int c=0; cout<<c++<<c;

Well, technically it isn't properly undefined until the next revision
of the standard. As it is the standard's examples are inconsistent
with the normative text, which therefore has some shadow of doubt
hanging over it. But in practice the consensus is that it's
undefined, and the reason is that 'c' is modified and used with no
intervening sequence point; a sequence point is a point where all
computations specified before the point are guaranteed to have finished.

You can do

cout << c++;
cout << c;

but better,

cout << c;
++c;
cout << c;

See <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_11.html>.
 
R

Ron Natalie

Victor said:
It's not undefined, it's unspecified. What you have is:

(cout.operator<<(c++)).operator<<(c);

The compiler can choose to extract the value of 'c' for the
second call before it increments 'c' as the result of ++.

V
Nope, it's undefined. An allowable ordering of the expression
modifes the value and uses it for some other purpose between
sequence points.
 
V

Victor Bazarov

Ron said:
Nope, it's undefined. An allowable ordering of the expression
modifes the value and uses it for some other purpose between
sequence points.

A function call has as sequence point before and after it. Did
you account for that?

V
 
O

Old Wolf

Victor said:
A function call has as sequence point before and after it. Did
you account for that?

Yes. The 'c++' and the 'c' could both be evaluated before
any function call is made. The '.' doesn't introduce any
ordering; this example is essentially no different to: foo(c++, c)
 
R

Ron Natalie

Victor said:
A function call has as sequence point before and after it. Did
you account for that?

V
Yes, but an allowable ordering is to evaluate the parameters
of both operator<< calls PRIOR to calling either function.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top