Doubt on cout Functinality

N

naveen.dixit

I have a piece of code

#include<iostream>
using namespace std;
main()
{
int x=12;
int *p = &x;
cout<<endl<<p<<endl<<*p++<<endl;
cout<<endl<<p<<endl<<*p<<endl;
}

but the output on G++ 3.4.2 compiler on linux is:
0xfef30ee0 //value of p (address of x)
12

0xfef30ee0 // again the same value of p( after
increementing)
0 // value at p is changed this time

Can anyone plz resolve this query?
 
C

Charles Bailey

I have a piece of code

#include<iostream>
using namespace std;
main()
{
int x=12;
int *p = &x;
cout<<endl<<p<<endl<<*p++<<endl;
cout<<endl<<p<<endl<<*p<<endl;
}

but the output on G++ 3.4.2 compiler on linux is:
0xfef30ee0 //value of p (address of x)
12

0xfef30ee0 // again the same value of p( after
increementing)
0 // value at p is changed this time

Can anyone plz resolve this query?

Undefined behaviour. In cout<<endl<<p<<endl<<*p++<<endl there is no
guarantee that the value of p when it appears in the <<p<<p part of
the expression is determined before the side effects of the ++
operator in *p++ has occurred. In fact there is no guarantee that the
value of p will be evaluated as any useful value or that demons won't
fly out of your nose.
 
R

Ron AF Greve

Hi,

The problem with your code is that the order in which the pieces are
evaluated are undefined!

In your first line the address that is printed in <<p<< is AFTER the
increment so *p++ is evaluated first (first p is dereferenced then p is
increased then the dereferenced value is printed i.e. 12)
int *p = &x;
cout<<endl<<p<<endl<<*p++<<endl;


Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
R

Robert Bauck Hamar

I have a piece of code

#include<iostream>
using namespace std;
main()

int main()
{
int x=12;
int *p = &x;
cout<<endl<<p<<endl<<*p++<<endl;
cout<<endl<<p<<endl<<*p<<endl;
}

but the output on G++ 3.4.2 compiler on linux is:

My g++ gives (about) same answer.
0xfef30ee0 //value of p (address of x)

This is not the address of x. Check.
12

0xfef30ee0 // again the same value of p( after
increementing)
0 // value at p is changed this time

Can anyone plz resolve this query?

Because when arguments are evaluated (the order of evaluation) are
unspecified, g++ chose to first evaluate *p++, giving the result 12, and
then p, giving now &x + 1 because p had already been incremented. Since
another order of evaluation gives different results, this is undefined
behaviour, and anything could happen.

If you turn on warnings, g++ will tell you this (I believe, mine is newer):
$ g++ -Wall -ansi -pedantic test.cc
test.cc:3: error: ISO C++ forbids declaration of 'main' with no type
test.cc: In function 'int main()':
test.cc:7: warning: operation on 'p' may be undefined

The first line tells you that you should use "int main", and the two next
tells you that line 7 may have undefined behaviour.

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16
 
P

Pete Becker

Ron said:
The problem with your code is that the order in which the pieces are
evaluated are undefined!

Technically, the order of evaluation is unspecified, not undefined. The
difference is that the former doesn't make a program ill-formed, and the
latter does.

--

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

Gavin Deane

I have a piece of code

#include<iostream>
using namespace std;
main()
{
int x=12;
int *p = &x;

Check in your debugger here what the value of p is. I bet it's not
0xfef30ee0, the value that gets output twice. I wouldn't be surprised
if the value is sizeof int* (probably 4 or 8) less than 0xfef30ee0.
cout<<endl<<p<<endl<<*p++<<endl;

The statement above is a series of calls to operator<< functions. It
is equivalent to

cout.operator<<(endl).operator<<(p).operator<<(endl).operator<<(*p+
+).operator<<(endl);

The compiler has to call the functions in the right order, but it is
allowed to evaluate the arguments to all those operator<< function
calls *before* it calls any of them and *in any order it likes*. In
your case it happens to have chosen to evaluate the argument to the
fourth call (*p++) *before* evaluating the argument to the second call
(p) so the value of p that is displayed by the call operator<<(p) is
the value *after incrementing*.

You can force the compiler to evaluate p before evaluting *p++ by
putting the evaluations in separate statements. Change the one
statement above to

cout<<endl<<p<<endl;
cout<<*p++<<endl;

and run the program again. You should see the output you I think were
originally expecting.
cout<<endl<<p<<endl<<*p<<endl;

By the time we get to this statement, p has been incremented and
points to nowhere so the *p here has undefined behaviour. In practice,
the output you are seeing from this line is the value of p after
incrementing (which is also what the previous statement displayed),
followed by whatever your compiler happened to do in this case of
undefined behaviour.
}

but the output on G++ 3.4.2 compiler on linux is:
0xfef30ee0 //value of p (address of x)
12

0xfef30ee0 // again the same value of p( after
increementing)
0 // value at p is changed this time

Gavin Deane
 
G

Gennaro Prota

Technically, the order of evaluation is unspecified, not undefined. The
difference is that the former doesn't make a program ill-formed, and the
latter does.

Are you sure? I don't think the technical meaning of "ill-formed"
includes programs which invoke undefined behavior.
 
J

James Kanze

I have a piece of code
#include<iostream>
using namespace std;
main()
{
int x=12;
int *p = &x;
cout<<endl<<p<<endl<<*p++<<endl;

Undefined behavior: you both modify p, and read it other than
for determining the new value.
cout<<endl<<p<<endl<<*p<<endl;

Undefined behavior: p doesn't point to anything legal, so
dereferencing it is illegal.
but the output on G++ 3.4.2 compiler on linux is:
0xfef30ee0 //value of p (address of x)
12
0xfef30ee0 // again the same value of p( after
increementing)
0 // value at p is changed this time
Can anyone plz resolve this query?

The output could be just about anything. Or the program could
core dump. Or reformat your hard disk. (I can imagine
different things that might actually be occuring in this case,
but why bother? The behavior might be different the next time
you run the program.)
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top