int or size_t ?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat May 7 13:49:23 PDT 2011


Actually my example was bad. What I wanted to say is that size_t will
be 64bit on 64bit platforms while int will stay 32bit. Another
difference is that size_t is unsigned. So it's bad to use int even if
you're sure you're only going to compile only on 32bit platforms.

Here's the relevant definitions in object_.d:

version(X86_64)
{
    alias ulong size_t;
    alias long  ptrdiff_t;
    alias long  sizediff_t;
}
else
{
    alias uint  size_t;
    alias int   ptrdiff_t;
    alias int   sizediff_t;
}

And an example:
void main()
{
    size_t val = size_t.max;    // maximum unsigned 32bit number on
32bit platforms

    int val2 = val;
    writeln(val2);              // -1 on 32bit platforms, due to
unsigned -> signed conversion
}

Usually people type "int" because.. well it's used everywhere and it's
easy to type. I never liked the "size_t" name, but it has become a de
facto standard in other languages, so D uses that name as well. We've
already had a discussion on changing its name but nothing came out of
it.


More information about the Digitalmars-d-learn mailing list