Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | What does it mean to evaluate a VLA? |
| Date | 2012-12-18 17:28 -0800 |
| Organization | None to speak of |
| Message-ID | <lnr4mmrftq.fsf@nuthaus.mib.org> (permalink) |
N1370 6.5.3.4p2, discussing the sizeof operator, says:
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.
What does it mean to "evaluate" the operand of sizeof when it's of a
variable length array type?
An example:
int n = 42;
volatile int vla[n];
printf("sizeof vla = %zu\n", sizeof vla);
Without the "volatile", the only thing the compiler *needs* to evaluate
is (probably) the hidden internal variable that records the length of
the VLA. A strict reading of 6.5.3.4p2 implies that it must evaluate
the array expression itself, which presumably requires accessing the
elements of the array (since they're declared volatile). I doubt that
that was intended (or likely even considered, since it's a rather odd
corner case).
Another example:
printf("%zu\n", sizeof (char[printf("Hello, world\n")]));
Here the operand is a parenthesized type name, which must be "evaluated"
-- but as far as I know there's no definition of evaluating a type name.
Clearly we need to evaluate any *expressions* contained within the type
name in order to be able to determine its size, which means that both
printf() calls will be executed.
I suggest that this reflects the intent:
If the operand is an expression of a variable length array type,
the expression is not evaluated, but the result is not an integer
constant expression. If the operand is a parenthesized name of
a variable length array type, any expressions within the type
name are evaluated. Otherwise, the operand is not evaluated
and the result is an integer constant expression.
Note also that this changes the final phrase from "integer constant" to
"integer constant expression"; see my earlier post here:
Message-ID: <lnip8zmud9.fsf@nuthaus.mib.org>
https://groups.google.com/forum/?fromgroups=#!topic/comp.std.c/wLso961KBZI
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.std.c | Previous | Next — Next in thread | Find similar | Unroll thread
What does it mean to evaluate a VLA? Keith Thompson <kst-u@mib.org> - 2012-12-18 17:28 -0800
Re: What does it mean to evaluate a VLA? Shao Miller <sha0.miller@gmail.com> - 2012-12-18 21:09 -0500
Re: What does it mean to evaluate a VLA? Shao Miller <sha0.miller@gmail.com> - 2012-12-20 03:52 -0500
Re: What does it mean to evaluate a VLA? Shao Miller <sha0.miller@gmail.com> - 2012-12-20 04:08 -0500
Re: What does it mean to evaluate a VLA? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 20:43 -0800
Re: What does it mean to evaluate a VLA? Shao Miller <sha0.miller@gmail.com> - 2012-12-21 05:01 -0500
Re: What does it mean to evaluate a VLA? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-21 08:55 -0800
csiph-web