6

I'm confused about size_t. I know it's an unsigned type..right? My question is, when should it be used. Is there a reason why it should be used with a regular array? I mean one would have to declare the array size to be really huge, so huge that a regular unsigned or signed wouldn't be able to handle it. And then a size_t would be able to deal with it right? Can someone give me an example?

mort
  • 12,988
  • 14
  • 52
  • 97
Hugo Perea
  • 465
  • 4
  • 15
  • 1
    I should always use size_t with dynamic structures such as dynamic structures, lists, vectors, etc.. rite? I'm just confused about a regular array – Hugo Perea Aug 15 '15 at 05:31
  • size_t araySize = 88888888888888888888888888; for (size_t count = 0; size_t < araySize; count++) is this a good reason to use it since the size is really big – Hugo Perea Aug 15 '15 at 05:32
  • Please refer http://stackoverflow.com/questions/1951519/when-to-use-stdsize-t – user258367 Aug 15 '15 at 05:35
  • 4
    The inclusion of unsigned types in C (and hence C++) was a design error which has led to many bugs and not much benefit. I avoid using them whereever possible. – john Aug 15 '15 at 05:35
  • @Hugo Perea: `size_t`, as any other type, has a limit. You are not allowed to go over that limit just because you want to. Your `88888888888888888888888888` is definitely too large for most modern platforms (32 and 64 bit ones). And yes, it is an unsigned type. You should use unsigned types whenever you can, and use signed types only when you really have to. – AnT stands with Russia Aug 15 '15 at 05:36
  • 4
    Or use signed types whenever you can, and unsigned types only when you really have to. (you'll find no consensus here) – Benjamin Lindley Aug 15 '15 at 05:43
  • 1
    @john, without an explanation, that statement of yours isn't helpful, in particular not to someone who is obviously a beginner. – Ulrich Eckhardt Aug 15 '15 at 05:43
  • Would it be practical to use in a member function that simply returns the number of employees? without any fancy data structures. – Hugo Perea Aug 15 '15 at 05:44
  • I will just ask more questions when I start data structures class – Hugo Perea Aug 15 '15 at 05:46
  • @BenjaminLindley Well I could write a long explanation but I'd only get flamed. I only wanted to make the OP aware that there are alternative points of view. But if the OP is interested then here's a page of integer overflow bugs, https://www.owasp.org/index.php/Integer_overflow example 3 is a signed/unsigned bug. – john Aug 15 '15 at 05:55
  • @john: Was that comment meant for me? Or Ulrich? Because I agree with you. Well, not necessarily that unsigned types shouldn't exist. But definitely that they should not be used for many things that people use them for, such as storing integers. – Benjamin Lindley Aug 15 '15 at 06:00

4 Answers4

4

According to ISO IEC 14882:2011(E)

§ 18.2 6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.

That makes std::size_t the natural choice when writing library code that deals with dynamic allocation, particularly with arrays and containers that manage arrays.

However in your own code, if you know your array is never going to be larger than 15-20 elements (for example) then there is no need to work with std::size_t wide values if you prefer to use something smaller.

Galik
  • 47,303
  • 4
  • 80
  • 117
2

TL;DR

Do not use unsigned for size/index unless you're forced to for some reason. In C/C++ unsigned does NOT mean non-negative.

The face that size_t is an unsigned type in the language and standard library is a design mistake explainable only with the historical context of the time when the decision was taken (CPUs were 16-bit back then but RAM was getting large).

See

https://stackoverflow.com/a/3260179/320726 https://stackoverflow.com/a/24104825/320726 https://stackoverflow.com/a/18248537/320726 https://stackoverflow.com/a/31089063/320726 https://stackoverflow.com/a/30799874/320726 https://stackoverflow.com/a/3029941/320726

for more details and examples of bugs that using an unsigned type to represent a quantity may introduce...

PS: Unfortunately for reasons that are not clear to me, saying something totally obvious like that the difference of two non-negative values can be negative is seen as a personal offence by a curiously high percentage of C/C++ "experts". If you like to be with the majority more than you like to be right then probably jumping on the "size_t being unsigned is the best idea since sliced bread" bandwagon is a viable option.

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265
  • 6
    This is just pontificating about a matter of opinion. Unsigned *does* mean non-negative and my opinion is that unsigned types should be preferred and size_t should be unsigned. – M.M Aug 15 '15 at 06:58
  • @MattMcNabb: the problem is that the **language semantic** doesn't support your view and therefore unsigned DOES NOT mean non-negative (and this is not my opinion). Unless of course you think that the difference of two non-negative values is non-negative... but in that case your brain is not logic and discussing logically with you is just a waste of time. – 6502 Aug 15 '15 at 08:30
1

size_t is an abstraction. You should not be worrying about which primitive type it maps to. Every container has its own size_type type which is usually used to convey the size of the container. In most cases it is a typedef of the allocator class used in the container. Many containers use unsigned primitives but I have seen many external libraries just using signed int as their size_type It is simply an attempt to standardize the size types used in containers. The purpose of size_t is not to be used with a native array. It is meant to be used to return the size of a container or anything that has a "size". You most certainly don't want to be making a 'native' array so big that it has more elements than the maximum value of an int

  • Gotcha. So if I have a member function that returns a private member that represents the number of time a linked list node has been created..I use size_t. However, what if I simply have a class that stores employees info without any fancy data structure, is this also a good reason to use size_t. I'm not using it to return the size of a "container" but rather a simple variable that counts. – Hugo Perea Aug 15 '15 at 05:41
1

Lots of very good answers... But to keep it simple

According to ISO IEC 14882:2011(E)

§ 18.2 6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.

Use it, if you know for sure that you are dealing with unsigned integers. For most part you array indexes fit the bill.

Secondly it is useful when code needs to be platform neutral. Like on a 16bit processor vs 32bit vs 64bit processor unsigned integer will be of different size and hence better to use size_t. Hence extensively used in libraries.

Vic
  • 1,985
  • 1
  • 19
  • 30