'int' is enough for 'length' to migrate code from x86 to x64

Frank Like via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 21 01:12:33 PST 2014


> Here's a simple program to calculate the relative size of two 
> files, that will not work correctly with unsigned lengths.
>
> module sizediff;
>
> import std.file;
> import std.stdio;
>
> void main(string[] args)
> {
>    assert(args.length == 3, "Usage: sizediff file1 file2");
>    auto l1 = args[1].read().length;
>    auto l2 = args[2].read().length;
>    writeln("Difference: ", l1 - l2);
> }

This will be ok:

writeln("Difference: ", (l1 >l2)? (l1 - l2):(l2 - l1));

If 'length''s type is not 'size_t',but is 'int' or 'long', it 
will be ok like this:

import std.math;
writeln("Difference: ", abs(l1 >l2));

Mathematical difference between unsigned value,size comparison 
should be done before in the right side of the equal sign 
character.

If this work is done in druntime,D will be a real system language.


More information about the Digitalmars-d mailing list