Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum size of size_t

I know in C return type of sizeof operator is size_t being unsigned integer type defined in <stdint.h>. Which means max size of it should be 65535 as stated in C99 standard 7.18.3:

limit of size_t
  SIZE_MAX             65535

However in gcc-4.8.2 header file stdint.h has defined its size much greater than 65535 contradicting to which is stated in C99 standard as below shown,

/* Limit of `size_t' type.  */
# if __WORDSIZE == 64
#  define SIZE_MAX              (18446744073709551615UL)
# else
#  define SIZE_MAX              (4294967295U)
# endif

Kindly help me in understanding why there is a difference or reason behind my misinterpretation.

like image 333
Sunil Bojanapally Avatar asked Mar 19 '14 18:03

Sunil Bojanapally


People also ask

Is Size_t bigger than long?

On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.

Is Size_t the word size?

No; size_t is not necessarily whatever you mean by 'the word size' of the machine that will run the code (in the case of cross-compilation) or that compiled the code (in the normal case where the code will run on the same type of machine that compiled the code).


1 Answers

The standard says that SIZE_MAX must be at least 65535.

It specifies no upper bound, and gcc's implementation is perfectly valid.

Quoting the reference you cited (emphasis added):

Its implementation-defined value shall be equal to or greater in magnitude (absolute value) than the corresponding value given below, with the same sign.

like image 174
Keith Thompson Avatar answered Oct 22 '22 05:10

Keith Thompson