Matrix creation quiz
Don
nospam at nospam.com
Fri Apr 29 14:44:11 PDT 2011
bearophile wrote:
> Pedro Rodrigues:
>
>> The fact that 'i' and 'j' are deduced to type 'uint' in the second
>> version. That's the kind of bug that would keep me up at night.
>
> Almost right answer. i and j are size_t, that is not uint in 64 bit compilations. Unsigned numbers cause the (i-j) sub-expression to give wrong results.
>
> ------------------------
>
> Moritz Warning:
>
>> I wonder, can there be done smth. on behalf of the language to prevent
>> this kind of bug?
>
> Two possible solutions, both refused by Walter:
> - Dmd may use signed word for array indexes and lenghts.
Yes -- but see below.
> - dmd may introduce runtime overflows.
That would not fix this problem. You're doing arithmetic on unsigned
values, where overflow doesn't happen.
Solution 3:
Dmd could use a special size_t type internally, defined as an integer of
range equal to the address space. Internally, the compiler would view it
as a long of range 0..cast(long)uint.max.
Thus, although it would implicitly convert to uint, it would not have
uint semantics (size_t*size_t would no longer convert to uint).
But it wouldn't be an int, either. ( int a; if (a>b.length).. would be a
signed/unsigned mismatch).
Incidentally a size_t type would allow us to catch bugs like:
uint n = a.length;
-- which compiles happily on 32 bits, but won't compile on a 64 bit
system. I think it should be rejected on all systems.
More information about the Digitalmars-d-learn
mailing list