delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/10/31/08:04:07

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
From: "Dave Korn" <dave DOT korn AT artimi DOT com>
To: <cygwin AT cygwin DOT com>
References: <000201c93ac7$38265930$4001a8c0 AT mycomputer> <490A30C8 DOT 5000107 AT sh DOT cvut DOT cz> <001601c93b31$a961b940$4001a8c0 AT mycomputer> <003e01c93b42$e92a17a0$4001a8c0 AT mycomputer> <490AE8A0 DOT 8090009 AT sh DOT cvut DOT cz> <001a01c93b4d$617de150$4001a8c0 AT mycomputer> <490AF1E3 DOT 3020308 AT sh DOT cvut DOT cz> <002001c93b52$3b2b2490$4001a8c0 AT mycomputer> <028e01c93b53$53e17a60$9601a8c0 AT CAM DOT ARTIMI DOT COM> <004501c93b55$1c2e11d0$4001a8c0 AT mycomputer>
Subject: RE: cygwin g++ strictness
Date: Fri, 31 Oct 2008 13:03:25 -0000
Message-ID: <02a201c93b59$0f3c2da0$9601a8c0@CAM.ARTIMI.COM>
MIME-Version: 1.0
X-Mailer: Microsoft Office Outlook 11
In-Reply-To: <004501c93b55$1c2e11d0$4001a8c0@mycomputer>
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

John Emmas wrote on 31 October 2008 12:35:

> ----- Original Message -----
> From: "Dave Korn"
> Sent: 31 October 2008 12:22
> Subject: RE: cygwin g++ strictness
>> 
>> You are creating temporaries here.  If AddTwoInts modifies either of the
>> int references it has, that will only change the temporaries; x and y
>> will /not/ be modified.
> 
> You'll be surprised if you try it Dave....

  Ah, I overlooked that you were using plain int32_t types in this example.
It wouldn't work with gints (in fact, contrary to what I said, the compiler
won't even create temps for you), not even if gint is a typedef for int32_t.
Your example requires gint to be a typedef for int.

  If gint is a typedef for long, and you make 'm' a gint as well as 'n', and
compile on a platform where longs are 64 bits and ints 32, the assignment to
'a' in AddTwoInts will overwrite both n and m in main (simulated here by
typedef'ing gint to long long on a 32-bit platform):


~ $ cat woo.cxx
#include <stdio.h>
typedef long long gint;

int AddTwoInts (int& a, int& b)
{
    int x = a;
    int y = b;

    a = 6;  // Note this line

    return x + y;
}

int main()
{
gint  m=4;
int  n=5;

    AddTwoInts ((int&)m, n);
    printf ("m is %d n is %d\n", m, n);

    return 0;  // 'm' equals 6 by the time you get to this line !!
}
@_______. .
(       /"\
 ||--||(___)
 '"  '"'---'
~ $ g++ woo.cxx -o woo
@_______. .
(       /"\
 ||--||(___)
 '"  '"'---'
~ $ ./woo.exe
m is 6 n is 0
@_______. .
(       /"\
 ||--||(___)
 '"  '"'---'
~ $


  That's why the casts are inadvisable.

  Add "--save-temps" to your compiler flags to get the preprocessed sources in
*.i or *.ii (for C or C++) files.


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


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