Should I use size_t?
What is the purpose of the type size_t? I've seen it used a few times in the standard library, and I know it is basically an unsigned int or something, but when should I use it? As far as I can tell, it would be easier in all cases to just use int so I don't have to include any extra headers.
size_t is the type of the value returned by the sizeof() statement, which means that it's guarentteed to be able to represent all possible sizes of any single object on the target machine. This translates to being the word-size of the machine, so on 32-bit processors this will be an unsigned 32-bit integer and similarly for 16-bit, 64-bit, whatever. The reason it gets used instead of int is because
* It's unsigned, so removes the possibility of users specifying negative sizes
* int IS NOT guarentteed to be equal to the machine's word size, in fact there are no guarenttees whatsoever about it's absolute size, only that sizeof(short)<=sizeof(int)<=sizeof(long)
* It's unsigned, so removes the possibility of users specifying negative sizes
* int IS NOT guarentteed to be equal to the machine's word size, in fact there are no guarenttees whatsoever about it's absolute size, only that sizeof(short)<=sizeof(int)<=sizeof(long)
You should use size_t any time you have a variable that describes a number of elements - characters in a string, size of an array, whatever.
The type is made available so that you can be descriptive about the sort of data that is stored in your variables. It's definitely a good idea to make use of expressive types like size_t whenever possible, as it removes ambiguity and clarifies your intentions.
The type is made available so that you can be descriptive about the sort of data that is stored in your variables. It's definitely a good idea to make use of expressive types like size_t whenever possible, as it removes ambiguity and clarifies your intentions.
Well, I'll take your word for it and use size_t, but I still don't understand what is so significant about the machine's word size in describing a number of elements.
Also, which header should I include for size_t? Previously I was using it from the <memory> header along with auto_ptr, but I don't think that's the proper way to include it.
Also, which header should I include for size_t? Previously I was using it from the <memory> header along with auto_ptr, but I don't think that's the proper way to include it.
Quote:Original post by Mr Awesome
Well, I'll take your word for it and use size_t, but I still don't understand what is so significant about the machine's word size in describing a number of elements.
Let's say you have a platform with 32-bit memory addressing, but an int is only 16 bits large. If sizeof returned an int instead, it would not be able to correctly report sizes larger than 64 KB, but it would be possible to create such object due to the addressing being much larger than that of an int. The purpose of size_t is to be large enough to indicate the size of anything, because int does not have any guarantees to do that.
Maybe more a teoretical problem than a practical one on 64-bit platforms in the same situation as above, but still, the problem is there and must be covered in some way.
Quote:
Original post by ApochPiQ
You should use size_t any time you have a variable that describes a number of elements - characters in a string, size of an array, whatever.
Are you sure about that?
Whenever you're describing the size of a region in memory, or an offset from an address, you should use size_t. So the sizeof(int[50]) should be size_t, but for int[index], can't index can be whatever counting value you desire, such as "int".
Using size_t as your counting value would, however, ensure that you will run out of virtual memory before you roll over the number of objects in your array.
That's only my 1c worth tho, feel free to correct me if what I quoted is actually how it was designed.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement