Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: sizeof on an uninitialized pointer to a VLA Date: Sat, 21 Nov 2015 12:09:40 -0800 Organization: None to speak of Lines: 55 Message-ID: References: <5e03af2c-3c3e-4432-bf5a-000867e5febb@googlegroups.com> <87mvu8rbor.fsf@bsb.me.uk> <871tbjpr4m.fsf@bsb.me.uk> <81b2f3fc-c570-4daf-850b-237640f1fd22@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="32152"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gxx2csPXqpAWyLxmc62JX" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:ZIYR4wW9vNXEh0ZF2vZziGyQLa0= sha1:I8FuoTzBziKKzOWAouRF2ryKhTc= Xref: csiph.com comp.lang.c:76798 supercat@casperkitty.com writes: > On Saturday, November 21, 2015 at 5:27:02 AM UTC-6, Keith Thompson wrote: >> 2. sizeof ( vla_type ) >> Not a constant expression. Any expressions that are part of the type >> name are evaluated > > What if one has a VLA of pointers to a VLA type? Would expressions forming > part of the name of the thing being pointed to be evaluated? Good question. A concrete example: #include int five(void) { puts("five()"); return 5; } int six(void) { puts("six()"); return 6; } int main(void) { printf("%zu\n", sizeof (char(*[five()])[six()])); } On my system (where pointers are 8 bytes), the result of the sizeof expression is 40, and both five() and six() are called. The call to six() is not necessary to determine the result of the sizeof expression. (I'm not sure whether it's possible for the size to vary depending on the value returned by six().) I suggest that all expressions that are part of the type name should be evaluated, just for the sake of keeping the description simple. The standard could, I suppose, specify which expressions are needed to determine the size and which are not, but it would be complex, potentially ambiguous, and not worth the effort. I could live with it being unspecified if that would make things easier for some compilers. If I wanted to do something similar to the above in real-world code, I would almost certainly define a typedef, so the issue wouldn't arise: typedef char(*funky_array[five()])[six()]; printf("%zu\n", sizeof (funky_array)); -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"