473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Mar 24 '07 #1
3 7720
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
25
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library...
21
by: Martin Wells | last post by:
Someone posted the following excerpt recently in relation to the sizeof operator: 6.5.3.4p2: "... If the type of the operand is a variable length array type, the operand is evaluated; otherwise,...
29
by: lovecreatesbea... | last post by:
The following function determines the maximum of two integers. It works on my machine. If (a - a) is negative, what's the first bit of: (unsigned)(a - a)? Is it 0 or 1? #include <limits.h>...
5
by: Paul | last post by:
hi, there, I was asked such a question: how to determine the size of memory of the int pointer pointed? for example int GetTheSizeofMemory(int *buffer) { int size; //enter your code here,...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
21
by: Bill Cunningham | last post by:
I have create these 2 files. Called main.c and atr.c. They seem to work pretty well. I just wanted to submit them to see what if any errors others that know more might find. Thanks. atr.c ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.