Wording glitch: sizeof array vs. sizeof (array)
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 15 Jun 2006 18:51:29 GMT
I've just thought of a rather odd consequence of the wording of the
standard.
C99 6.5.1p5:
A parenthesized expression is a primary expression. Its type and
value are identical to those of the unparenthesized expression. It
is an lvalue, a function designator, or a void expression if the
unparenthesized expression is, respectively, an lvalue, a function
designator, or a void expression.
C99 6.3.2.1p3:
Except when it is the operand of the sizeof operator or the unary
& operator, or is a string literal used to initialize an array, an
expression that has type "array of type" is converted to an
expression with type "pointer to type" that points to the initial
element of the array object and is not an lvalue. If the array
object has register storage class, the behavior is undefined.
Assume the following declaration:
int array[100];
I'll quotation marks are used to delimit expressions, not to denote
string literals.
We know, of course, that in "sizeof array", the expression "array" is
not converted to a pointer, so "sizeof array" yields the size of the
array.
What about "sizeof (array)"? "array" by itself is an expression of
array type. It's not the operand of the sizeof operator; the operand
of the sizeof operator is "(array)", not "array". In the terms of
6.5.1p5, the unparenthesized expression is "array", whose type (after
the implicit conversion) is int*, and whose value is the address of
the first element of the array. The parenthesized expression has the
same type and value. Applying sizeof to the parenthesized expression
should therefore yield the size of the pointer. So:
sizeof array == sizeof(int[10])
sizeof (array) == sizeof(int*)
Now this is obviously not the intent. We all know that "sizeof expr"
and "sizeof (expr)" mean the same thing, and that in the latter case
"expr" is treated as the operand of the sizeof operator, and as far as
I know all actual C compilers treat it that way (fortunately!). But
the standard doesn't *quite* say this.
Obviously a parenthesized expression is intended to have the same
attributes (type, value, lvalue-ness, etc.) as the unparenthesized
expression. The problem, I think, is that the standard's definition
of the semantics of a parenthesized expression attempts to enumerate
those attributes, but it only lists a limited number of them:
Type
Value
Is it an lvalue?
Is it a function designator?
Is it a void expression?
and misses others:
Is it the operand of a sizeof operator?
Is it the operand of a unary & operator?
Is it a null pointer constant? (I've mentioned this one before.)
Anything else I haven't thought of.
By listing a finite set of attributes, it implies that it doesn't
apply to any other attributes that aren't listed.
There are several possible ways to deal with this.
One is to ignore it and assume that everyone knows what it *really*
means. It's likely that this is what's actually going to happen, but
I'm not very comfortable with it.
Another is to expand 6.5.1p5 so it covers more attributes that a
parenthesized expression inherits from the unparenthesized expression.
This would make the paragraph move verbose, and could still miss some
things that might be significant.
Yet another is to describe what we all understand a parenthesized
expression *really* means: that it retains all the attributes of the
unparenthesized expression. (The most important ones could be listed
in a footnote, but it should be clear that the list isn't necessarily
exhaustive.) A formal definition of "attribute" would be nice, but I
don't know how to write one that wouldn't be circular, and I think the
word is clear enough by itself. (Perhaps something needs to be said
about stringizing.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: Wording glitch: sizeof array vs. sizeof (array)
- From: Douglas A. Gwyn
- Re: Wording glitch: sizeof array vs. sizeof (array)
- From: David R Tribble
- Re: Wording glitch: sizeof array vs. sizeof (array)
- Prev by Date: Boost C
- Next by Date: Re: Wording glitch: sizeof array vs. sizeof (array)
- Previous by thread: Boost C
- Next by thread: Re: Wording glitch: sizeof array vs. sizeof (array)
- Index(es):
Relevant Pages
|
Loading