sizeof
This issue has been automatically converted from the original issue lists and some formatting may not have been preserved.
Authors: Clive D.W. Feather <clive@demon.net>, UK C Panel
Date: 2001-09-07
Submitted against: C99
Status: Closed
Converted from: summary-c99.htm, dr_266.htm
Consider the following code:
char x [SIZE_MAX / 2][SIZE_MAX / 2];
size_t s = sizeof x;
The size of x
cannot be fitted into an object of type size_t
. Assuming that
SIZE_MAX
is 65535, what is the value of s
? More generally, which of the
following is, or should be, the case ?
(SIZE_MAX + 1)
.sizeof
with such a large argument.SIZE_MAX
bytes.6.5.3.4#2 says in part:
[#2] The
sizeof
operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer.
Note that there is no indication that the result may be other than the correct size.
One of:
If the size is too large to fit in an object of type
size_t
, it is converted to that type in the manner described in subclause 6.3.1.3.
If the size is too large to fit in an object of type
size_t
, it is replaced by an implementation-defined value.
[#1a] The
sizeof
operator shall not be applied to an operand whose size, in bytes, is larger than the maximum value of the typesize_t
.
The implementation shall ensure that the type
size_t
is large enough to hold the result of all uses of thesizeof
operator.
[Some of these are less than wonderful, and consideration should also be given to the interaction with VLAs.]
Comment from WG14 on 2004-03-06:
The committee has deliberated and decided that more than one interpretation is reasonable. Translation limits do not apply to objects whose size is determined at runtime.
sizeof(a[SIZE_MAX/2][SIZE_MAX/2]);
The program is not strictly conforming because it exceeds an environmental
limit.
If the implementation generates code, there is no requirement for a diagnostic.
In the event that sizeof
is called on the object, a diagnostic should be
issued, but not required.
VLAs are a special case.
The program is not strictly conforming because it exceeds an environmental
limit. If the implementation generates code, there is no requirement for a
diagnostic. In the event that sizeof
is called on the object, a diagnostic can
be issued, but is not required.