By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,900 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,900 IT Pros & Developers. It's quick & easy.

Standard way to determine max range of size_t?

P: n/a
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
Mar 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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_limits<std::size_t>::max()

Cheers! --M

Mar 24 '07 #2

P: n/a
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_limits<std::size_t>::max()

Cheers! --M
Thank you. This is exactly what I needed.

- Michael
Mar 24 '07 #3

P: n/a
"Michael Ekstrand" <us****@elehack.netwrote in message
news:eu**********@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.
Mar 24 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.