Why size_t is used instead of unsigned int

Hey guys,

just a curiosity. In JUCE’s codebase I noticed a lot of size_t in place of unsigned int… for instance, to access an array element. Is there a reason for that? I always used unsigned ints when I need an integer positive value for any reasons.


size_t is defined to be able to hold the maximum address that a pointer can have on the target platform. So it makes sense to use it whenever you have e.g. a container that holds elements addressed by a single pointer.

Because of this, the C++ STL uses this type quite heavily. And if you interface with STL types, the most straightforward thing is to use the types on the interfaces without any casting, so the usage of size_t will propagate. My guess is that this explains most of the usages that you can find in the JUCE codebase.

1 Like