Why is size_t unsigned?

JS js.mdnq at gmail.com
Sun Jul 21 21:36:50 PDT 2013


On Monday, 22 July 2013 at 03:58:31 UTC, Ali Çehreli wrote:
> On 07/21/2013 08:47 PM, JS wrote:
>
> > Doing simple stuff like
> >
> > for(int i = 0; i < s.length - 1; i++) fails catastrophically
> if s is
> > empty. To make right one has to reduce performance by writing
> extra checks.
>
> Checks are needed for program correctness. If not in source 
> code, in compiler generated code, or the microprocessor itself. 
> The compiler and the microprocessor would not do such things 
> for performance reasons. It is because sometimes only the 
> programmer knows that the check is unnecessary.
>
> > There seems to be no real good reason why size_t is
> unsigned...
>
> How about, every addressable memory locations must be countable?

for strings themselves, I would prefer an int to be returned. The 
size of a string has nothing to do with it's location in memory.

>
> > Surely one doesn't require too many strings larger than 2^63
> bits on an x64
> > os...
>
> Agreed.
>
> > I running into a lot of trouble because of the way D deals
> with implicit
> > casting of between signed and unsigned.
>
> D is behaving the same way as C and C++ there.

No, surely not... Well, at least, I never had this trouble in C#.

> > please don't tell me to use foreach... isn't not a panacea.
>
> I would still prefer foreach because it is more convenient and 
> safer because of needing less code.
>
> Ali

foreach doesn't allow you to modify the index to skip over 
elements.





More information about the Digitalmars-d-learn mailing list