Re: [eigen] Indexes: why signed instead of unsigned? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Indexes: why signed instead of unsigned?
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Tue, 11 May 2010 09:30:48 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=AP0BF72hP7C+5gH8TznONBz5hYjsRvNnvrYhuSZH5qM=; b=njtVe471JvaJSE2GnHu7jZpaiN3T3SRq2am8igt5BgqQR+5bbTbP/Pl90k80bHYFzw 86nt0FjXqp9OkcIMcUY/qZoHPvKeOlyum8mp73cshZzgz7L0YiVAavQEGFhYjLo1qII/ ANnalkPrrF8v4/8nS6sVDYeWyo4+0+Nc746wA=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=pbmTVSbdR1CglOZYC6sqeP7ZMHaWmJN+gf8gL8a+kzHpHG4eihGiEZ3jQhiRdRm3Ej OBiS5+QFKVxtl0P6Py/3L50/kSITqggwv3nxS5FJJfxHpz12O5FPbfKEalnP3bKbgwjn JVFwf2YmNG/TX2z9OdhotnVAeUC23tj+QEzkk=
Thanks Mark for supporting my position (in favor of signed) with fresh
examples, when I myself was starting to doubt!
What about the other debate, between in (32bit) and ptrdiff_t (same
size as void*) ?
Benoit
2010/5/11 Mark Borgerding <mark@xxxxxxxxxxxxxx>:
> Using unsigned types for indexing is much less forgiving of different styles
> of for loops ( as your example shows ). I've been programming since I was
> twelve (nearly 30 years now) and I still often write "for" loops that don't
> work well with unsigned types. The loops look correct and in most cases
> behave correctly.
>
> Here are a few examples that fail with unsigned N:
>
> for (k=0;k<N-3;k+=4)
> // ... deal with 4 elements at a time. // This fails
> spectacularly with unsigned N<3. It works with signed N
>
> while (--N > 0) {
> // fails with unsigned N=0. It works with signed N
> }
>
> Even trickier, there can be latent bugs that only appear when
> sizeof(unsigned int) != sizeof(void*) (this one really happened to me)
> unsigned int smallOffset = N-k
> buf[ largeOffset + smallOffset ] = 42;
> If unsigned N<k then smallOffset wraps around to around 4e9 (with a 32 bit
> int). On a 32 bit OS, the index/dereference of buf wraps back around, doing
> what the programmer intended (latent bug).
> On a 64 bit OS, this actually has the address space to offset buf by 4e9
> elements and fails spectacularly.
> If smallOffset and N were signed, then there would be no problem.
>
>
> -- Mark Borgerding
>
>
> Benoit Jacob wrote:
>>
>> There's a new thread on the forum about that (sigh).
>> http://forum.kde.org/viewtopic.php?f=74&t=87883
>>
>> Let's settle this once and for all. There are 2 debates:
>> 1) signed or unsigned?
>> 2) int (that is 32 bits) or same-sizeof-as-pointers (e.g. 64bit on
>> 64bit platforms)?
>>
>> For 2) signed, we could do ptrdiff_t, i guess. Unless you're sure that
>> 'long' is actually on all platforms the size of a pointer. I still
>> dont know whether 2) matters. Certainly not for cubic-complexity
>> algorithms (would take forever). But for plain "level 1" operations,
>> perhaps it's plausible. Feels like we shouldn't arbitrarily restrict
>> sizes to 32 bits. Opinions?
>>
>> For 1), I don't know. the forum poster mentions a reasonable way to
>> write decreasing for loops. It's true that such loops are not too
>> frequent anyway. I dont know. Asking for opinions. Dont want to impose
>> upon you a decision made 3 years ago when I was a "noob".
>>
>> Gael? Hauke? Jitse? Thomas? everybody?
>>
>> Benoit
>>
>>
>
>
>
>