length's type.

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jan 18 04:30:33 UTC 2024


On Wednesday, January 17, 2024 7:55:37 PM MST zjh via Digitalmars-d-learn 
wrote:
> Can you change the type of 'length' from 'ulong' to 'int', so I
> haven't to convert it every time!

If you mean for arrays, length and array indices are specifically size_t so
that their size will match the pointer size for the architecture. On 64-bit
systems, that means that it's ulong (whereas on 32-bit systems, it would be
uint). If it were int, then you couldn't access all of the elements of
larger arrays (and arrays will get that large in some cases - e.g. when
dealing with larger files). C/C++ does the same thing.

If you want your code to be portable and to be able to handle larger arrays,
then it should be using size_t for array indices and length and not int, in
which case, you're not typically going to need to convert from ulong to int,
because you'd just be using size_t, which would then be ulong on 64-bit
systems. Obviously, when you do need to convert to int, then that can be
annoying, but for a lot of code, using auto and size_t makes it so that you
don't need to use int, and it would be a big problem in general if the
language made length int.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list