Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old March 24th, 2007, 05:25 PM
Michael Ekstrand
Guest
 
Posts: n/a
Default 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
  #2  
Old March 24th, 2007, 05:25 PM
mlimber
Guest
 
Posts: n/a
Default Re: Standard way to determine max range of size_t?

On Mar 24, 12:18 pm, Michael Ekstrand <use...@elehack.netwrote:
Quote:
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

  #3  
Old March 24th, 2007, 06:25 PM
Michael Ekstrand
Guest
 
Posts: n/a
Default Re: Standard way to determine max range of size_t?

On Sat, 24 Mar 2007 09:23:56 -0700, mlimber wrote:
Quote:
Quote:
>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
  #4  
Old March 24th, 2007, 09:05 PM
Andrew Koenig
Guest
 
Posts: n/a
Default Re: Standard way to determine max range of size_t?

"Michael Ekstrand" <usenet@elehack.netwrote in message
news:eu3j17$n7p$1@aioe.org...
Quote:
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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.