A simple question: is variably-modified type a VLA (variable length array) only?
C11, 6.10.8.3 Conditional feature macros, 1 (emphasis added):
__STDC_NO_VLA__
The integer constant1
, intended to indicate that the implementation does not support variable length arrays or variably modified types.
Does it mean that there is a variably-modified type, other than VLA? Any examples?
What is the relationship between "variably modified type" and "variable length array"?
Extra: the definition of "variable length array" depends on the definition of "known constant size":
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; otherwise, the array type is a variable length array type.
However, the definition of "known constant size" depends on the definition of "variable length array":
A type has known constant size if the type is not incomplete and is not a variable length array type.
A bit confused.
Related DR: http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_312.htm.
CodePudding user response:
Does it mean that there is a variably-modified type, other than VLA? Any examples?
According to the C Standard (6.7.6 Declarators)
3 A full declarator is a declarator that is not part of another declarator. If, in the nested sequence of declarators in a full declarator, there is a declarator specifying a variable length array type, the type specified by the full declarator is said to be variably modified. Furthermore, any type derived by declarator type derivation from a variably modified type is itself variably modified.
Here is a demonstration program
#include <stdio.h>
int main( void )
{
for ( size_t n = 1; n < 10; n )
{
typedef int ( *Ptr )[n];
Ptr p;
printf( "sizeof( *p ) = %zu\n", sizeof( *p ) );
}
}
The program output is
sizeof( *p ) = 4
sizeof( *p ) = 8
sizeof( *p ) = 12
sizeof( *p ) = 16
sizeof( *p ) = 20
sizeof( *p ) = 24
sizeof( *p ) = 28
sizeof( *p ) = 32
sizeof( *p ) = 36
In this program the pointer type Ptr
defined like int( * )[n]
is a variably modified type.
This quote
A type has known constant size if the type is not incomplete and is not a variable length array type.
means that the sizeof
operator for such types is evaluated at compile-time opposite to the evaluation at run-time for variable length array types and the size of such a type is not changed during the program execution.
The quote has a different meaning relative to the quote below
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; otherwise, the array type is a variable length array type.
that says about how distinguish a declaration of a variable length array from a declaration of a non-variable length array.
CodePudding user response:
C 2018 6.7.6 3 says:
If, in the nested sequence of declarators in a full declarator, there is a declarator specifying a variable length array type, the type specified by the full declarator is said to be variably modified. Furthermore, any type derived by declarator type derivation from a variably modified type is itself variably modified.
Therefore int (*)[n]
, for some non-constant n
, is a variably modified type even though it is a pointer. Further, int [3][n]
is a variably modified type.