可变修饰的A型VLA仅是VLA吗?

发布于 2025-01-18 07:19:58 字数 770 浏览 2 评论 0原文

一个简单的问题:可变修改类型是否仅是 VLA(可变长度数组)?

C11,6.10.8.3 条件特征宏,1(添加强调):

__STDC_NO_VLA__ 整数常量1,旨在指示 实现不支持可变长度数组可变 修改类型。

这是否意味着除了 VLA 之外还存在可变修改类型?有什么例子吗?

“可变修饰类型”和“可变长度数组”之间有什么关系?


补充:“变长数组”的定义取决于“已知常量大小”的定义:

如果大小是整数常量表达式 并且元素类型具有已知的常量大小,数组类型不是可变长度 数组类型;否则,数组类型为变长数组类型。

然而,“已知常量大小”的定义取决于“可变长度数组”的定义:

如果类型不是不完整且不是可变长度数组类型,则该类型具有已知常量大小

有点困惑。

相关灾难恢复: http://www.open-std .org/jtc1/sc22/wg14/www/docs/dr_312.htm

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 constant 1, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城之時有危險 2025-01-25 07:19:59

除了VLA以外,这是否意味着有变化的类型?
有任何例子吗?

根据C标准(6.7.6宣言者)

3完整的声明器是一个不是另一个的声明者
声明人。如果,在完整的声明序列中
声明器,有一个声明器指定可变长度数组
类型,据说由完整声明者指定的类型是可变的
修改的。此外,声明器类型推导得出的任何类型
从可变的修改类型中,本身是可变的。

这是一个演示程序,

#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 ) );
    }
}

程序输出

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

在此程序中,指针类型ptr定义为int( *)[n]是一种可变的修改类型。

这个报价

如果类型不完整并且是
不是可变长度阵列类型。

意味着,在程序执行过程中,在运行时与运行时的评估相反,在编译时与运行时的评估相反,对此类类型的运算符进行sizeof

报价相对于下面的报价具有不同的含义

如果大小是整数常数,并且元素类型具有
已知常数大小,数组类型不是可变长度阵列
类型;否则,数组类型是可变长度数组类型。

这说明了如何区分可变长度阵列的声明与声明不变长度阵列的声明。

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.

渔村楼浪 2025-01-25 07:19:59

C 2018 6.7.6 3说:

如果在完整声明器中的声明器的嵌套序列中,有一个指定变量长度数组类型的声明器,则完整声明器指定的类型被认为是可变修改的。此外,从可变修改类型的声明剂类型派生派生的任何类型本身都是可变的。

因此,int(*)[n],对于某些非恒定n,即使它是指针,也是一种可变的修改类型。此外,int [3] [n]是一种可变的修改类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文