public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: "Jay Krell" <jay.krell@cornell.edu>
To: "swe sd" <ccwork@hotmail.com>
Cc: <cygwin@sourceware.cygnus.com>
Subject: Re: problem in C++ pointer
Date: Sun, 12 Mar 2000 00:06:00 -0000	[thread overview]
Message-ID: <000f01bf8bfa$2bb5e8d0$0201a8c0@jayk_home4nt> (raw)

The behavior at a different time and place of code with undefined behavior
is not entirely relevant. Different processors, different command lines,
different optimizations (possibly processor specific), different versions,
can lead to different definitions of undefined behavior. Still, sometimes
undefined behavior is predictable.

I do not yet know if this code is undefined, but certainly the outcome of
code like
i = i++;
i = ++i;
i = i--;
i = --i;
printf("%d%d", i, ++i);
printf("%d%d", --i, ++i);
etc.

is. If it isn't clear if code falls into this pattern, that's probably
enough reason to not write code like it. But even so, if it is defined, it
is a bug if Gcc doesn't consistently give it the standard definition.

Here's a good imho boiled down example:
class X
{
public:
    X* F(int i) { printf("%d", i); return this;}
};

int i = 0;
X x, *px = &x;
px->F(i++)->F(i++)->F(i++);

is the expected output 012 or is it undefined?
Could it reasonably be 000, if none of the storages occur till after all
parameters are evaluated but before any functoins are called?

according to the parts of the standard I can find and decipher:
[expr] 5.0.2: "..uses of overloaded operators are transformed into function
calls as described in 13.5. Overloaded operators obey rules for syntax
specifidd in this clause, but the requirements of operand type, lvalue, and
evaluation order are replaced by the rules for function call".

I couldn't find the evaluation order for function calls except

[intro.execution]1.8.17 "..There is also a sequence point after the copying
of a returned value and before the execution of any expressions outside the
function". I don't know if that "copying of a returned value" applies to
pointers and references, and I find "expressions outside the function"
unclear.

Ordinarily, the sides of a -> I don't believe have a defined order of
evaluation:

eg:
class C
{
public:
int i;
C(int i) { this->i = i ; }
void F(C* c) { printf("%d%d", this->i, c->i); }
};

C carray[2] = { C(0), C(1) };
C* pc = carray;

(pc++)->F(pc++);

Is the output defined?
It could "reasonably" be
01
10
00
?

I'm assuming the order of eval of an unoverloaded -> is the same, if
defined, as the order of eval of ".".

"sequence point" is not in the index..

 - Jay

-----Original Message-----
From: swe sd <ccwork@hotmail.com>
To: jay.krell@cornell.edu <jay.krell@cornell.edu>
Cc: cygwin@sourceware.cygnus.com <cygwin@sourceware.cygnus.com>
Date: Saturday, March 11, 2000 9:20 PM
Subject: Re: problem in C++ pointer


>  I do not agree. I execute the same program on a UNIX station with gcc
>compiler and it works fine ... ie the output of line 7 is
>same as the output of line 9.
>
>>From: "Jay Krell" <jay.krell@cornell.edu>
>>To: "swe sd" <ccwork@hotmail.com>, <cygwin@sourceware.cygnus.com>
>>Subject: Re: problem in C++ pointer
>>Date: Sat, 11 Mar 2000 16:31:28 -0800
>>
>>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@hotmail.com>
>>To: cygwin@sourceware.cygnus.com <cygwin@sourceware.cygnus.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@sourceware.cygnus.com

             reply	other threads:[~2000-03-12  0:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-03-12  0:06 Jay Krell [this message]
  -- strict thread matches above, loose matches on Subject: below --
2000-03-17 13:26 Heribert Dahms
2000-03-17 13:15 Jay Krell
2000-03-17  9:48 Jay Krell
2000-03-16  8:39 James Stern
2000-03-13  2:07 Jay Krell
2000-03-13  1:54 Bernard Dautrevaux
2000-03-12 19:51 Jay Krell
2000-03-12  7:52 swe sd
2000-03-11 21:20 swe sd
2000-03-11 16:33 Jay Krell
2000-03-11 16:29 Jay Krell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000f01bf8bfa$2bb5e8d0$0201a8c0@jayk_home4nt' \
    --to=jay.krell@cornell.edu \
    --cc=ccwork@hotmail.com \
    --cc=cygwin@sourceware.cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).