Objects of known constant size

Jens Gustedt, INRIA and ICube, France

2025-02-27

target

integration into IS ISO/IEC 9899:202y

document history

document number date comment
n3391 202411 Original proposal
n3416 202412 Simplify according to feedback on the reflector
n3498 202502 Some more word-smithing
n3508 202502 Some more word-smithing,
use array length expression instead of size

1 Motivation

It seems that the current terminology that is applied for variable length arrays (VLA) and variably modified types (VM types) is often misunderstood because the term “variable length array type” is introduced quite confusingly spread over the text and by applying double negation. We think that the dual term, “complete type with known constant size”, is much easier to introduce consistently.

The goal of this papers to strictly define the term “complete type with known constant size” recursively. VLA then are just “complete array types for which the size is not known during translation”.

This is not intended to change semantics of the text.

Whether or not we then keep up the term VLA (to which people are accustomed) and whether or not we use size/length/extend/count… is of secondary interest and could be done in a second phase.

2 Suggested additions and changes to the wording.

New text is underlined green, removed text is stroke-out red.

2.1 In 6.2.4, Storage durations of objects

6 For such an object that does not have a variable length array type has a known constant size, see 6.2.5, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial representation of the object is indeterminate. If an initialization is specified for the object and it is not specified with constexpr, it is performed each time the declaration or compound literal is reached in the execution of the block; if it is specified with constexpr the initializer is evaluated once at translation time and the new instance of the object is initialized to that fixed value each time the specification is reached; otherwise, the representation of the object becomes indeterminate each time the declaration is reached.

7 For such an object that does have a variable length array type not have a known constant size, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration.26) If the scope is entered recursively, a new instance of the object is created each time. The initial representation of the object is indeterminate.

2.2 In 6.2.5 (Types)

28 A complete type shall have a size that is less than or equal to SIZE_MAX. A type has known constant size if it is complete and is not a variable length array type.FNT)

FNT) That is, any complete type that is not an array type has a known constant size. For types that are not derived, for pointer types and for atomic types this follows directly from their definitions. For structure and union types this is because they are composed of members that all have a known constant size, see 6.7.3.2. For array types specific rules apply to determine if they have known constant size, see 6.7.7.3.

2.3 In 6.5.4.5, The sizeof and alignof operators

2 The sizeof operator yields the size (in bytes) of its operand, which can 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 does not have a known constant size, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant expression.

2.4 In 6.7.3.2 Structure and union specifiers

3 Each member of a structure or union shall not contain a member with incomplete or function type have a known constant size (hence …

Note to the editors: It might be good to profit from the change to transform the weird subsentences in parenthesis that are obviously not meant to be part of the normative text into footnotes.

11 A member of a structure or union can have any complete object type type of known constant size other than a variably modified type.132)

2.5 In 6.7.7.3, Array declarators

Constraints

1 In addition to optional type qualifiers and the keyword static, the [ and ] can delimit an expression or *. If they delimit an expression, (which specifies the size of an array) called the array length expression, the expression shall have an integer type.

4 If the size array length expression is not present, the array type is an incomplete type. If the sizethere is * instead of being an array length expression, the array type is a variable length array type of unspecified sizearray length, which can only be used as part of the nested sequence of declarators or abstract declarators for a parameter declaration, not including anything inside an array sizelength expression in one of those declarators;161) such arrays are nonetheless complete types. If the size array length expression is an integer constant expression and the element type has a known constant size, the array type is not a variable length array typethe array type has a known constant size; otherwise, the array type is a variable length array type.FNT)

FNT)(Variable length arrays with automatic storage duration are a conditional feature that implementations may are not required to support; see 6.10.10.4.)

5 If the size is an expression that array length expression is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero. The size of each instance of a variable length array type does not change during its lifetime. Where a size an array length expression is part of the operand of the typeof or sizeof operators and changing the value of the size array length expression would not affect the result of the operator, it is unspecified whether or not the size array length expression is evaluated. Where a size an array length expression is part of the operand with a _Lengthof operator and changing the value of the size array length expression would not affect the result of the operator, the size array length expression is not evaluated. Where a size an array length expression is part of the operand of an alignof operator, that expression is not evaluated.

6 For two array types to be compatible, both shall have compatible element types, and if both size specifiers array length expressions are present, and are integer constant expressions, then both size specifiers array length expressions shall have the same constant value. If the two array types are used in a context which requires them to be compatible, the behavior is undefined if the two size specifiers array length expressions evaluate to unequal values.

3 Interaction with other proposals

Note that n3495 also proposes changes to 6.7.7.3, but that these are independent of each other.

Aknowledments

Thanks to Joseph Myers and Robert Seacord for review and discussions.