Submitter: UK C Panel
Submission Date: 2001-09-07
Source: Clive D.W. Feather <clive@demon.net>
Version: 1.0
Date: 2001-09-07
Subject: overflow of sizeof
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 ?
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 type size_t.
The implementation shall ensure that the type size_t is large enough to hold the result of all uses of the sizeof operator.
[Some of these are less than wonderful, and consideration should also be given to the interaction with VLAs.]