Article View: alt.os.linux.mandrake
Article #500g++ problem in LM7.2
From: Chun Jia
Date: Mon, 11 Dec 2000 02:54
Date: Mon, 11 Dec 2000 02:54
94 lines
3291 bytes
3291 bytes
Hi,
I have tried a simple problem to understand pointers but found the cout
give me a wrong output in my LM7.2 of a PIII box where the version on
the Sun workstation give me the right output. So there must be sth wrong
with the LM7.2's g++ compiler.
The code is as below:
//test.cpp. compile with g++, run with a.out
#include<iostream>
int main(){
int *p=new int[10];
for (int i=0; i<10; i++){
*(p+i)=i*2;
cout<<"p"<<i<<" "<<*(p+i)<<" "<<(p+i)<<endl;
}
cout<<"p= "<<p<<endl
<<"*p= "<<*p<<endl
<<"p++= "<<p++<<endl
<<" *p++= "<<(*p++)<<endl
<<" p= "<<p<<endl
<<" *p= "<<*p<<endl;
}
In my box the above program give me a output as:
$a.out
p0 0 0x8049b60
p1 2 0x8049b64
p2 4 0x8049b68
p3 6 0x8049b6c
p4 8 0x8049b70
p5 10 0x8049b74
p6 12 0x8049b78
p7 14 0x8049b7c
p8 16 0x8049b80
p9 18 0x8049b84
p= 0x8049b68
*p= 4
p++= 0x8049b64
*p++= 0
p= 0x8049b60
*p= 0
$
Please note that the point p's value should be 0x8049b60 instead of
0x8049b68. The later p++ in the cout actually has affected the p in the
first place. Actually, if I place extra cout after each endl, the output
is correct. To make sure, I also run the above code in a SUN workstation
also using g++ compiler. It gives me a right output as below:
$a.out
p0 0 0x43960
p1 2 0x43964
p2 4 0x43968
p3 6 0x4396c
p4 8 0x43970
p5 10 0x43974
p6 12 0x43978
p7 14 0x4397c
p8 16 0x43980
p9 18 0x43984
p= 0x43960
*p= 0
p++= 0x43960
*p++= 2
p= 0x43968
*p= 4
$
On the sun, the g++ -v and uname -a gives a output as
$g++ -v
Reading specs from
/opt/gcc-2.95.2/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/specsgcc version
2.95.2 19991024 (release)
$uname -a
SunOS expert.cc.purdue.edu 5.7 Generic_106541-12 sun4u sparc
SUNW,Ultra-Enterprise
$
On my box, the g++ -v and uname -a gives me a output as:
$g++ -v
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux/2.95.3/specs
gcc version 2.95.3 19991030 (prerelease)
$uname -v
Linux pvil-a-070.resnet.purdue.edu 2.2.17-21mdk #1 Thu Oct 5 13:16:08
CEST 2000
i686 unknown
$
Any response welcome.
Technical Details
Message-ID:
<3A344203.2C41523D@purdue.edu>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-03!supernews.com!newsfeed.direct.ca!look.ca!logbridge.uoregon.edu!arclight.uoregon.edu!mozo.cc.purdue.edu!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10ba33,22878daa04232d2b,start X-Google-Attributes: gid10ba33,public X-Google-Thread: f8a83,22878daa04232d2b,start X-Google-Attributes: gidf8a83,public X-Google-ArrivalTime: 2000-12-10 18:50:06 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-03!supernews.com!newsfeed.direct.ca!look.ca!logbridge.uoregon.edu!arclight.uoregon.edu!mozo.cc.purdue.edu!not-for-mail From: Chun Jia <jiac@purdue.edu> Newsgroups: alt.os.linux.mandrake,gnu.g++.help Subject: g++ problem in LM7.2 Date: Sun, 10 Dec 2000 21:54:59 -0500 Organization: Purdue University Lines: 94 Message-ID: <3A344203.2C41523D@purdue.edu> NNTP-Posting-Host: pvil-a-070.resnet.purdue.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: mozo.cc.purdue.edu 976502864 25501 128.211.252.70 (11 Dec 2000 02:47:44 GMT) X-Complaints-To: usenet@mozo.cc.purdue.edu NNTP-Posting-Date: 11 Dec 2000 02:47:44 GMT X-Mailer: Mozilla 4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) X-Accept-Language: en