Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1784

Re: What does it mean to evaluate a VLA?

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.std.c
Subject Re: What does it mean to evaluate a VLA?
Date 2012-12-21 05:01 -0500
Organization A noiseless patient Spider
Message-ID <kb1c2p$iru$1@dont-email.me> (permalink)
References <lnr4mmrftq.fsf@nuthaus.mib.org> <kfnpq24816y.fsf@x-alumni2.alumni.caltech.edu>

Show all headers | View raw


On 12/20/2012 23:43, Tim Rentsch wrote:
> Keith Thompson <kst-u@mib.org> writes:
>
>> 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?
>
> The address is computed (including evaluating any subexpressions
> necessary to do that, which could include evaluating an array
> declarator dimension expression in a type-name).  Since the array
> is an operand of sizeof, nothing further is done with the
> computed address.
>

Mentioned elsethread is the 'register' case.  I can't think of why the 
address would ever need to be computed.

>> An example:
>>
>>      int n = 42;
>>      volatile int vla[n];
>>      printf("sizeof vla = %zu\n", sizeof vla);
>>
>> [...]
>
> [...]
>
>> 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.
>
> Right.  AFAIK there is no text in the Standard that says clearly
> and unambiguously that type-names in expressions must be evaluated
> when the containing expression is.  About the best I could find is
> reasoning that such a type-name is an operand of its larger
> expression, and value computations of operands must precede the
> value computation of the containing expression.  It seems clear
> there is an expectation that array declarators in such type-names
> be evaluated when their containing expression is, but I don't
> think that any text in the Standard says that directly.
>
> [...]
> The key issue here is evaluation of expressions in type-names.
> The description of sizeof is not the right place to give that;
> probably it should go either in the section on Array Declarators
> or the section on Type Names (whatever those numbers are).  Once
> it is established that expressions in type-names must be evaluated
> as part of evaluating their containing expression, which I believe
> is always true, eg, in casts, then the stuff in sizeof is okay.
>
> [...]
>

C was so much simpler before VLAs... :)

If expressions in type-names contribute to the size of the type, how can 
they not be evaluated?  Whether they're evaluated at translation-time or 
at run-time would seem to depend on what contributes to the size, with 
VLAs being the only run-time case.

Not that anyone asked, but personally, I find the Standard to be 
straight-forward on this matter.  Having said that, perhaps this would 
be clearer (opinions?):

   "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. If 
the type of the operand is a variable length array type, the operand is 
evaluated as a void expression; otherwise, only those sub-expressions of 
the operand that are required in order to determine its size are 
evaluated and the result is an integer constant."

   int foo[42 ? 1 : 3];

- Shao Miller

Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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