53

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.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46

1 Answers1

64

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.

Rufflewind
  • 8,545
  • 2
  • 35
  • 55
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    Now it makes sense. `SIZE_MAX 65535` is minimum limit, which I got confused with `limit of size_t` thinking it as maximum limit. Thanks – Sunil Bojanapally Mar 19 '14 at 18:36
  • 5
    @SunEric: Right, `65535` is the minimum maximum. – Keith Thompson Mar 19 '14 at 18:46
  • 2
    Suspect `SIZE_MAX` upper bound is `UINTMAX_MAX`. – chux - Reinstate Monica Mar 19 '14 at 18:49
  • 2
    @chux: `SIZE_MAX` can't be bigger than `UINTMAX_MAX`, but there's no specified upper bound on `UINTMAX_MAX`. (It's only slightly less useful to say that the upper bound of `SIZE_MAX` is `SIZE_MAX`.) – Keith Thompson Mar 19 '14 at 18:50
  • 5
    Various limits in C often have a relationship such as `SIZE_MAX <= UINTMAX_MAX`. (likely all unsigned are so limited - yet many do not know this - hence my comment.) But some limits are not well ordered like `SIZE_MAX` compared to `LONG_MAX`. These 2 comes into interaction using `fseek()` (which returns long) to allocate a block to match a file using `malloc()` which takes a `size_t`. – chux - Reinstate Monica Mar 19 '14 at 18:57