Ordering comparisons

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 7 05:20:47 PST 2017


On 03/06/2017 10:44 PM, Nick Sabalausky (Abscissa) wrote:
> On 03/06/2017 08:27 PM, Andrei Alexandrescu wrote:
>>
>> * A struct S { int x; } compares differently on little endian and big
>> endian system (!)
>>
>
> This one is very surprising. How is that so, if both structs being
> compared are of the same endian-ness?

On a big endian system, comparing integers with memcmp works correctly 
because higher-rank bytes are compared before lower-rank bytes. On a 
little endian system, lower-rank bytes are compared first, which would 
make e.g. 256 less than 1. Porting code that relies on comparing arrays 
of structs for ordering is likely to be a nightmare. We really need to 
fix this. -- Andrei


More information about the Digitalmars-d mailing list