size_t vs int in C++ and/or C

后端 未结 9 905
南方客
南方客 2020-12-01 07:51

Why is it that in C++ containers, it returns a size_type rather than an int? If we\'re creating our own structures, should we also be encouraged to

相关标签:
9条回答
  • 2020-12-01 08:19

    A few reasons might be:

    • The type (size_t) can be defined as the largest unsigned integer on that platform. For example, it might be defined as a 32 bit integer or a 64 bit integer or something else altogether that's capable of storing unsigned values of a great length
    • To make it clear when reading a program that the value is a size and not just a "regular" int

    If you're writing an app that's just for you and/or throwaway, you're probably fine to use a basic int. If you're writing a library or something substantial, size_t is probably a better way to go.

    0 讨论(0)
  • 2020-12-01 08:22

    Some of the answers are more complicated than necessary. A size_t is an unsigned integer type that is guaranteed to be big enough to store the size in bytes of any object in memory. In practice, it is always the same size as the pointer type. On 32 bit systems it is 32 bits. On 64 bit systems it is 64 bits.

    0 讨论(0)
  • 2020-12-01 08:28

    In general, size_t should be used whenever you are measuring the size of something. It is really strange that size_t is only required to represent between 0 and SIZE_MAX bytes and SIZE_MAX is only required to be 65,535...

    The other interesting constraints from the C++ and C Standards are:

    • the return type of sizeof() is size_t and it is an unsigned integer
    • operator new() takes the number of bytes to allocate as a size_t parameter
    • size_t is defined in <cstddef>
    • SIZE_MAX is defined in <limits.h> in C99 but not mentioned in C++98?!
    • size_t is not included in the list of fundamental integer types so I have always assumed that size_t is a type alias for one of the fundamental types: char, short int, int, and long int.

    If you are counting bytes, then you should definitely be using size_t. If you are counting the number of elements, then you should probably use size_t since this seems to be what C++ has been using. In any case, you don't want to use int - at the very least use unsigned long or unsigned long long if you are using TR1. Or... even better... typedef whatever you end up using to size_type or just include <cstddef> and use std::size_t.

    0 讨论(0)
提交回复
热议问题