T
Tim Rentsch
Lowell Gilbert said:James Kuyper said:On 10/14/2011 11:10 AM, Lowell Gilbert wrote:
...
Does SIZE_MAX have to be the largest value representable in a size_t?
The standard just says that it's the "maximum value" for the type,
What distinction do you see between "largest value representable" if a
type and "maximum value" for a type? The phrases seem synonymous to me.
Certainly there's no difference in *most* cases.
For size_t, the type is just defined as the output type for sizeof,
which is defined in turn as yielding the size of its operand. So I can
see an argument that the "maximum value" of a size_t could be viewed as
the largest value that sizeof could possibly yield on the particular
implementation.
Well, the maximum value returnable by sizeof has to be the same as the
maximum value representable by size_t:
sizeof(char[(size_t)-1]) == (size_t)(-1)
Assuming I understand what you're getting at, you're begging the
question here. [But the expression doesn't make sense to me, so
the assumption may well be wrong.]
It has been argued that, since it's not possible for the standard's
specifications for the value and type of sizeof(char[SIZE_MAX][2]) to be
simultaneously satisfied, an implementation is free, among other
possibilities, to treat it as having a value that is larger than
SIZE_MAX. Personally, I think such code should be rejected, but it's not
clear that the standard requires, or even allows, it to be rejected.
Right; this argument has gone around a few times, and I don't think
there's a definitive answer either.
However, there's no possible basis that I'm aware of for rejecting
sizeof(char[(size_t)-1]).
I assume you mean SIZE_MAX rather than (size_t), in which case I agree.
But that doesn't mean SIZE_MAX has to be the largest value a size_t can
hold, if there are other limits on how big an object can be.
With respect to your claim "often is" - can you cite any implementation
where the equality expression given above is NOT true?
I think you're misreading my claim. There certainly are systems with
architectural limitations on how big an object can be, well short of
what fits in a size_t.
For example, the machine I'm posting from has 32-bit words, from which
it gets a 4-gigabyte range for its size_t. However, the operating
system reserves some of the address space, so no program can ever have
more than 3 gigabytes. As a result, sizeof can *never* return more than
3 gigabytes. This prompted me to wonder whether it would be legitimate
for the system's SIZE_MAX to be 3 gigabytes.
The type size_t is an unsigned integer type. The range of unsigned
integer types is specified in 6.2.6.1 p1; 3 gigabytes isn't one of
the possibilities. The range of unsigned types depends only on how
many value bits are present in their representation; nothing else.
That applies to size_t just as much as it does any other unsigned
integer type.
Does that help?