Page 1 of 1

When do you use size_t and int?

Posted: Thu Sep 25, 2014 9:44 pm
by lilzz
sometimes I see vars being declared as unsigned int and sometimes size_t. I traced size_t down and it is unsigned int in fact.

What's the point of declaring size_t?

Re: When do you use size_t and int?

Posted: Thu Sep 25, 2014 10:05 pm
by ShiftPlusOne
I don't know the specifics of this particular data type, but in general, such things are there for readability and portability. At a glance, you know it refers to the size of an object. It's an unsigned int on your platform, but perhaps it makes sense to use an unsigned long int elsewhere? In any case, you will be able to compile the same code on that other platform without having to change any variables.

Re: When do you use size_t and int?

Posted: Thu Sep 25, 2014 10:08 pm
by ShiftPlusOne

Re: When do you use size_t and int?

Posted: Fri Sep 26, 2014 12:13 pm
by Redrobes
That article doesnt seem to highlight the most important feature of size_t enough.

Size_t is always unsigned and at least 16 bits but otherwise its bit depth is not specified as part of the language. Neither is unsigned int for that matter. But it is not always the case that unsigned int and size_t have the same number of bits. The important thing tho is that size_t represents the compilers native working integer math size / register size / pointer size which in the case of 32 bit raspberry pi ARM core is 32 bits. If you take code badly written in 32bits and port to 64 bit compiler then it can be a pain and you can get lots of compiler warnings about the bit depths of the two being incompatible and possible data loss.

What I wanted to say in this post tho is that its not important what the bit depth of the types are but the thinking when using stuff like this. When you want a quick number with a small range like 1 to 100 then its ok to use an int or an unsigned int. For most things tho you should declare a new type by using a typedef and at that one point in time think about the bit depth you may need and then run with it.

For example if were talking about string length then its obviously unsigned but is it an int. Its not clear whether a string could be longer than an int can describe it. As such you will find that most calls to string length return size_t. In the standard library they go one better and have type defined as std::string::size_type. Usually this is set to size_t in the same way that size_t is usually set to unsigned int but its better to have the defined type for that specific thing and to let the compiler deal with that than trying for force the return from std::string::length() into anything other than std::string::size_type.

So stop trying to do the compilers job and define a type for what you want. Think about the range of values your type is going to need and define it once then use your custom defined type. If you have to convert that type into another then use a function and you can do all necessary range checking in there. If you use a C++ class for your type then you can override the operator to do that for you and hide that bit of the syntax in the code. But better that than to have to go through thousands of lines of code inserting casts to make it compile as usually by that time you have to guess what is going on in someone elses code and hope that the range will not break it.