delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2000/03/11/19:29:43

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>, <http://sourceware.cygnus.com/ml/#faqs>
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <002a01bf8bba$4fb89ce0$0201a8c0@jayk_home4nt>
From: "Jay Krell" <jay DOT krell AT cornell DOT edu>
To: "swe sd" <ccwork AT hotmail DOT com>, <cygwin AT sourceware DOT cygnus DOT com>
Subject: Re: problem in C++ pointer
Date: Sat, 11 Mar 2000 16:31:28 -0800
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3612.1700
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3612.1700

Your code might be triggering undefined behavior in C++, because of the
++X[0] in the same statement where you otherwise read X[0]. Definitely
something like
printf("%d%d", X[0], ++X[0]);
is undefined but I've read something along the lines that when the
operator's are actually overloaded, function calls, that the order of
evaluation becomes defined..

>     line 9:    cout<<A[0]<<" "<<&A<<" "<<&A[0]<<endl
>     line 10:       <<*X<<endl
>     line 11:       <<*X+5<<endl
>     line 12:       <<*X<<" "<<A[0]<<endl
>     line 13:       <<5+X[0]<<endl
>     line 14:       <<( X[0]==0 ? "X[0]=0" : "X[0]!=0")<<endl
>     line 15:       <<++X[0]<<endl

something like, skipping endl.
cout.<<(A[0]).<<(" ").<<(&A).<<(" ").<<(&A[0]).<<(*X).<<(*X+5).<<(*X).<<("
").<<A[0].<<(5+X[0]).<<(( X[0]==0 ? "X[0]=0" : "X[0]!=0")).<<(++X[0]);

 - Jay

-----Original Message-----
From: swe sd <ccwork AT hotmail DOT com>
To: cygwin AT sourceware DOT cygnus DOT com <cygwin AT sourceware DOT cygnus DOT com>
Date: Saturday, March 11, 2000 9:31 AM
Subject: B20: problem in C++ pointer


>   I compiled the following program (attachment test.cc):
>     line 1:  #include <iostream>
>     line 2:  void main()
>     line 3:  { const int size=10;
>     line 4:    int A[size];
>     line 5:    int *X=NULL, i;
>     line 6:    for (i=0;i<size;i++) A[i]=i;
>     line 7:    cout<<A[0]<<" "<<&A<<" "<<&A[0]<<endl;
>     line 8:    X=A;
>     line 9:    cout<<A[0]<<" "<<&A<<" "<<&A[0]<<endl
>     line 10:       <<*X<<endl
>     line 11:       <<*X+5<<endl
>     line 12:       <<*X<<" "<<A[0]<<endl
>     line 13:       <<5+X[0]<<endl
>     line 14:       <<( X[0]==0 ? "X[0]=0" : "X[0]!=0")<<endl
>     line 15:       <<++X[0]<<endl
>     line 16: }
>    and executing it gives output:
>           $./a.out
>           0 0x259fd7c 0x259fd7c
>           1 0x259fd7c 0x259fd7c
>           1
>           6
>           1 1
>           6
>           X[0]!=0
>           1
>    Obviously, the output of line 9 is different from line 7 which should
be
>the same indeed. Is there anything wrong ? Thanks.
>
>______________________________________________________
>Get Your Private, Free Email at http://www.hotmail.com
>


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019