Standard way to determine max range of size_t?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Ekstrand

    Standard way to determine max range of size_t?

    I'm looking for a standard (or standard-ish) way to determine the maximum
    value representable by a size_t. I can't seem to find anything
    officially standard - cstddef doesn't seem to define such a thing, nor
    does climits.

    Applying grep to my /usr/include reveals an stdint.h header which defines
    a SIZE_MAX, some further research indicates that this is a C99 standard
    header but not standard for either C89 or standard C++. My primary target
    compiler is G++, so that would be acceptable, but I would rather use a
    standard means if one exists.

    So, my question is: Is there a mechanism in standard C++ to determine the
    maximum value of a size_t? If there is not a define or some other
    declaration of this limit, is it reliable to assume that ((size_t) -1)
    (or some more appropriate style of cast) is the maximum value which can be
    stored in a size_t?

    Thank you,
    - Michael
  • mlimber

    #2
    Re: Standard way to determine max range of size_t?

    On Mar 24, 12:18 pm, Michael Ekstrand <use...@elehack .netwrote:
    I'm looking for a standard (or standard-ish) way to determine the maximum
    value representable by a size_t. I can't seem to find anything
    officially standard - cstddef doesn't seem to define such a thing, nor
    does climits.
    >
    Applying grep to my /usr/include reveals an stdint.h header which defines
    a SIZE_MAX, some further research indicates that this is a C99 standard
    header but not standard for either C89 or standard C++. My primary target
    compiler is G++, so that would be acceptable, but I would rather use a
    standard means if one exists.
    >
    So, my question is: Is there a mechanism in standard C++ to determine the
    maximum value of a size_t? If there is not a define or some other
    declaration of this limit, is it reliable to assume that ((size_t) -1)
    (or some more appropriate style of cast) is the maximum value which can be
    stored in a size_t?
    >
    Thank you,
    - Michael
    Yes. Include <limits>, then do:

    std::numeric_li mits<std::size_ t>::max()

    Cheers! --M

    Comment

    • Michael Ekstrand

      #3
      Re: Standard way to determine max range of size_t?

      On Sat, 24 Mar 2007 09:23:56 -0700, mlimber wrote:
      >So, my question is: Is there a mechanism in standard C++ to determine the
      >maximum value of a size_t? If there is not a define or some other
      >declaration of this limit, is it reliable to assume that ((size_t) -1)
      >(or some more appropriate style of cast) is the maximum value which can be
      >stored in a size_t?
      >
      Yes. Include <limits>, then do:
      >
      std::numeric_li mits<std::size_ t>::max()
      >
      Cheers! --M
      Thank you. This is exactly what I needed.

      - Michael

      Comment

      • Andrew Koenig

        #4
        Re: Standard way to determine max range of size_t?

        "Michael Ekstrand" <usenet@elehack .netwrote in message
        news:eu3j17$n7p $1@aioe.org...
        I'm looking for a standard (or standard-ish) way to determine the maximum
        value representable by a size_t.
        (size_t)-1 should do it. The point is that size_t is guaranteed to be an
        unsigned type, and a signed integer is converted to unsigned by taking it
        modulo 2^n, where n is the number of bits in the unsigned type.


        Comment

        Working...