By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Doubt on cout Functinality

P: n/a
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?

Jun 16 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

<na**********@gmail.comwrote in message
news:11**********************@a26g2000pre.googlegr oups.com...
>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?

Jun 16 '07 #2

P: n/a
On 2007-06-16, na**********@gmail.com <na**********@gmail.comwrote:
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.

Jun 16 '07 #3

P: n/a
na**********@gmail.com wrote:
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-lit...html#faq-39.16

--
rbh
Jun 16 '07 #4

P: n/a
Ron AF Greve wrote:
>
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)
Jun 16 '07 #5

P: n/a
On 16 Jun, 12:35, naveen.di...@gmail.com wrote:
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<<(end l).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

Jun 16 '07 #6

P: n/a
On Sat, 16 Jun 2007 08:12:55 -0400, Pete Becker wrote:
>Ron AF Greve wrote:
>>
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.
Are you sure? I don't think the technical meaning of "ill-formed"
includes programs which invoke undefined behavior.

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 16 '07 #7

P: n/a
Gennaro Prota wrote:
On Sat, 16 Jun 2007 08:12:55 -0400, Pete Becker wrote:
>Ron AF Greve wrote:
>>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.

Are you sure? I don't think the technical meaning of "ill-formed"
includes programs which invoke undefined behavior.
Too early in the morning. Undefined behavior does not make a program
ill-formed.

--

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

P: n/a
On Jun 16, 1:35 pm, naveen.di...@gmail.com wrote:
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.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 16 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.