Where do I find the definition of size_t?

前端 未结 9 2189
醉酒成梦
醉酒成梦 2020-11-28 04:37

I see variables defined with this type but I don\'t know where it comes from, nor what is its purpose. Why not use int or unsigned int? (What about other \"similar\" types?

9条回答
  •  心在旅途
    2020-11-28 05:02

    This way you always know what the size is, because a specific type is dedicated to sizes. The very own question shows that it can be an issue: is it an int or an unsigned int? Also, what is the magnitude (short, int, long, etc.)?

    Because there is a specific type assigned, you don't have to worry about the length or the signed-ness.

    The actual definition can be found in the C++ Reference Library, which says:

    Type: size_t (Unsigned integral type)

    Header:

    size_t corresponds to the integral data type returned by the language operator sizeof and is defined in the header file (among others) as an unsigned integral type.

    In , it is used as the type of the parameter num in the functions memchr, memcmp, memcpy, memmove, memset, strncat, strncmp, strncpy and strxfrm, which in all cases it is used to specify the maximum number of bytes or characters the function has to affect.

    It is also used as the return type for strcspn, strlen, strspn and strxfrm to return sizes and lengths.

提交回复
热议问题