www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - variations of int sizes (and signded/unsigned) between platorms

reply Jason House <jason.james.house gmail.com> writes:
I have a small section of code that is giving me issues moving between 
two machines.  On my amd64 machine, find returns a long and the slice 
operation expects ulongs.  On my friend's machine, find returns an int 
and refuses to accept cast(ulong)startComment.  Maybe it should be 
cast(uint)startComment?  Of course, that won't work on my machine.

Rather that try to decipher this with version checks (when I don't know 
how this varies from system to system), what's the proper way to solve 
this?  I'm using gdc on my amd64 machine.  I'm not sure if my friend is 
using gdc or dmd... or even if this is a variation form one version of 
phobos to another.

 $ dsss build
 housebot.d => housebot-0.6
 + /bin/rebuild -Idsss_imports/ -I. -S./ -I/include/d -S/lib/   -oqdsss_objs
-Dddoc -unittest -w -g housebot.d -ofhousebot-0.6
 warning - gtp.d:105: Error: implicit conversion of expression (startComment)
of type long to ulong can cause loss of data
 Command /bin/rebuild returned with code 256, aborting.
 
 $ head -n 108 gtp.d | tail
 
         private void getArgs(out char[][] args){
                 char[] line = inStream.readLine();
                 auto startComment = find(line, "#");
                 char[] commentFree = line;
                 if (startComment > -1)
                         commentFree = line[0..startComment];
                 // Remove starting whitespace, trailing whitespace
                 // Also replace sequences of spaces and tabs with a single
space
                 char[] squeezedLine = strip(squeeze(tr(line, "\t"," ")," "));

Apr 23 2007
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Jason House wrote:
 I have a small section of code that is giving me issues moving between
 two machines.  On my amd64 machine, find returns a long and the slice
 operation expects ulongs.  On my friend's machine, find returns an int
 and refuses to accept cast(ulong)startComment.  Maybe it should be
 cast(uint)startComment?  Of course, that won't work on my machine.
 
 Rather that try to decipher this with version checks (when I don't know
 how this varies from system to system), what's the proper way to solve
 this?  I'm using gdc on my amd64 machine.  I'm not sure if my friend is
 using gdc or dmd... or even if this is a variation form one version of
 phobos to another.
 
 $ dsss build
 housebot.d => housebot-0.6
 + /bin/rebuild -Idsss_imports/ -I. -S./ -I/include/d -S/lib/  
 -oqdsss_objs -Dddoc -unittest -w -g housebot.d -ofhousebot-0.6
 warning - gtp.d:105: Error: implicit conversion of expression
 (startComment) of type long to ulong can cause loss of data
 Command /bin/rebuild returned with code 256, aborting.

 $ head -n 108 gtp.d | tail

         private void getArgs(out char[][] args){
                 char[] line = inStream.readLine();
                 auto startComment = find(line, "#");
                 char[] commentFree = line;
                 if (startComment > -1)
                         commentFree = line[0..startComment];
                 // Remove starting whitespace, trailing whitespace
                 // Also replace sequences of spaces and tabs with a
 single space
                 char[] squeezedLine = strip(squeeze(tr(line, "\t","
 ")," "));


find should probably be returning ptrdiff_t, since that looks like what you're getting. If you try casting startComment to size_t on line 105, that should work. FYI: | 32-bit alias | 64-bit alias | size_t | uint | ulong | ptrdiff_t | int | long | Hope that helps. -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Apr 23 2007
parent reply Jason House <jason.james.house gmail.com> writes:
Daniel Keep wrote:
 find should probably be returning ptrdiff_t, since that looks like what
 you're getting.  If you try casting startComment to size_t on line 105,
 that should work.
 
 FYI:
           | 32-bit alias | 64-bit alias |
 size_t    |     uint     |     ulong    |
 ptrdiff_t |     int      |     long     |
 
 Hope that helps.
 
 	-- Daniel
 

It would help if the various distributions were consistent. Digging a little, it appears that gdc for AMD64 returns ptrdiff_t while the code that comes with dmd returns int. Also, http://www.digitalmars.com/d/phobos/std_string.html also says int.
Apr 24 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Jason House wrote:
 Daniel Keep wrote:
 find should probably be returning ptrdiff_t, since that looks like what
 you're getting.  If you try casting startComment to size_t on line 105,
 that should work.

 FYI:
           | 32-bit alias | 64-bit alias |
 size_t    |     uint     |     ulong    |
 ptrdiff_t |     int      |     long     |

 Hope that helps.

     -- Daniel

It would help if the various distributions were consistent. Digging a little, it appears that gdc for AMD64 returns ptrdiff_t while the code that comes with dmd returns int. Also, http://www.digitalmars.com/d/phobos/std_string.html also says int.

True. But then again, DMD only works on 32-bit archs, and since ptrdiff_t is an alias they're *technically* the same :3 I suppose that problems like this will disappear once DMD starts generating 64-bit executables, since it'll *have* to be addressed. I think it was Linus Torvalds who once remarked that porting something to lots of platforms tended to expose a lot of bugs just like the above :P -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Apr 24 2007
prev sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Jason House wrote:
 I have a small section of code that is giving me issues moving between 
 two machines.  On my amd64 machine, find returns a long and the slice 
 operation expects ulongs.  On my friend's machine, find returns an int 
 and refuses to accept cast(ulong)startComment.  Maybe it should be 
 cast(uint)startComment?  Of course, that won't work on my machine.
 
 Rather that try to decipher this with version checks (when I don't know 
 how this varies from system to system), what's the proper way to solve 
 this?  I'm using gdc on my amd64 machine.  I'm not sure if my friend is 
 using gdc or dmd... or even if this is a variation form one version of 
 phobos to another.
 

It seems like a version statement would be exactly the right thing to use in this case. Try something like this: version (X86_64) { alias ulong find_t; } else { alias uint find_t; } Then, in the slice, use a cast(find_t). -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Apr 24 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Kirk McDonald wrote:
 Jason House wrote:
 I have a small section of code that is giving me issues moving between 
 two machines.  On my amd64 machine, find returns a long and the slice 
 operation expects ulongs.  On my friend's machine, find returns an int 
 and refuses to accept cast(ulong)startComment.  Maybe it should be 
 cast(uint)startComment?  Of course, that won't work on my machine.

 Rather that try to decipher this with version checks (when I don't 
 know how this varies from system to system), what's the proper way to 
 solve this?  I'm using gdc on my amd64 machine.  I'm not sure if my 
 friend is using gdc or dmd... or even if this is a variation form one 
 version of phobos to another.

It seems like a version statement would be exactly the right thing to use in this case. Try something like this: version (X86_64) { alias ulong find_t; } else { alias uint find_t; } Then, in the slice, use a cast(find_t).

Or you could just use ptrdiff_t...
Apr 24 2007