473,769 Members | 6,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7803
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

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_li mits<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
5022
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 libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
24
3825
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
6547
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 as a small tool function according to its quality? I will be very happy to get your suggestion from every aspect on it: interface design, C language knowledge or algorithm efficient. Sincerely,
21
1399
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, the operand is not evaluated and the result is an integer constant." The first thing that occured to me was that nothing happens when you evaluate a VLA. I mean what's the following supposed to do?
29
3894
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> int max(int n1, int n2)
5
6321
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, to calculate the memory size of buffer
270
9533
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 to see that the answers to that message prove that programming exclusively in standard C is completely impossible even for a small and ridiculously simple program like the one I proposed. 1 I read the file contents in binary mode, what should...
36
3813
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 to read text or handle arrays of arbitrary length. I don't have the expertise in C of many folks here so I feel like I'm offering a small furry animal for sacrifice to a big armour plated one... but will offer it anyway. Please do suggest...
21
3267
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 #include <stdio.h> double atr (double th, double tl, double wc)
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.