On 11/25/13 21:54, Michael S. Tsirkin wrote: > On Tue, Nov 26, 2013 at 06:37:43AM +1000, Richard Henderson wrote: >> On 11/26/2013 06:31 AM, Michael S. Tsirkin wrote: >>> On Tue, Nov 26, 2013 at 06:24:53AM +1000, Richard Henderson wrote: >>>> On 11/25/2013 09:48 PM, Michael S. Tsirkin wrote: >>>>> g_string_vprintf was only introduced in 2.24 so switch to vsnprintf >>>>> instead. A bit uglier but name size is fixed at 4 bytes here so it's >>>>> easy. >>>> >>>> You list 2.24 here, >>>> >>>>> - GString *s = g_string_new(""); >>>>> + /* It would be nicer to use g_string_vprintf but it's only there in >>>>> 2.22 */ >>>> >>>> ... 2.22 here. >>>> >>>> But >>>> https://developer.gnome.org/glib/2.28/glib-Strings.html#g-string-vprintf >>>> >>>> says "since 2.14". >>>> >>>>> + char s[] = "XXXX"; >>>> >>>> char s[5]; >>>> >>>> Initializing it is a waste of time. >>>> >>>> >>>> r~ >>> >>> It's sets the length in a nice way. >>> >> >> Then do something like >> >> char s[sizeof("XXXX")]; >> >> so that the actual initialization doesn't happen. >> >> >> r~ > > Why? As an optimization? > I'm not quite sure this doesn't mean we are using VLA which I'd rather not. > Would need to look at language spec ... simple initialization is shorter > and more obviously correct.
No, it's not a VLA. C99 6.7.5.2 Array declarators, p4: [...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; [...] 6.6 Constant expressions, p6: An integer constant expression [...] shall have integer type and shall only have operands that are [...] sizeof expressions whose results are integer constants, [...] 6.5.3.4 The sizeof operator, p2: [...] The result is an integer. 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. 6.4.5 String literals, p2: [...] The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. [...] 6.7.5.2 Array declarators, p2: [...] If an identifier is declared to be an object with static storage duration, it shall not have a variable length array type. 6.7.5.2 Array declarators, p10: EXAMPLE 4 [...] Array objects declared with the static or extern storage-class specifier cannot have a variable length array (VLA) type. [...] So, char s[sizeof("XXXX")] ^^^^^^ string literal, static storage duration, not a VLA ^^^^^^^^^^^^^^ operand not evaluated, result is integer constant ... which qualifies as integer constant expression ^^^^^^^^^^^^^^^^^^^^^^ size is an integer constant expression, element type has a known constant size: not a VLA (Admittedly, EXAMPLE 4 in 6.7.5.2 Array declarators, p10, is informative (not normative), and 6.7.5.2 Array declarators, p2, speaks about an "identifier". We don't have an identifier for "XXXX", but I think we can still derive that static storage duration implies non-variable length for the array that holds the string.) Laszlo