为什么不能使静态数组的大小可变? [英] Why can't the size of a static array be made variable?

查看:35
本文介绍了为什么不能使静态数组的大小可变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论 C++ 时相关但不完全重复:
我们可以给静态数组的大小一个变量吗

我在其中一个子文件中定义了一个数组,如下所示.

I am defining an array in one of the child files as follows.

static int arr[siz];

这里的 siz 是子文件可用的全局变量.但是 gcc 编译器产生以下错误:

Here siz is a global variable available to the child file. But the gcc compiler produces the following error :

<filename>: <line_num> : error : storage size of ‘arr’ isn’t constant

为什么我不能定义可变大小的 static 数组?

Why can't I define a static array of variable size ?

EDIT :这似乎只是 static int 类型的问题.如果我将 arr 的变量类型从 static int 更改为 int,即使数组的大小仍然依赖于一个变量 siz.

EDIT : This seems to be a problem only for static int type. If I change the variable type of arr from static int to int, the error goes away, even though the size of array is still dependent on a variable siz.

推荐答案

由于您声明的数组的大小不是常量,因此您拥有的是一个可变长度数组(VLA).C99 标准允许使用 VLA,但存在一些相关限制.您不能拥有带有 staticextern 存储类说明符的可变长度数组.

Since the size of the array you declare is not constant, what you have is an Variable Length Array(VLA). VLA are allowed by the c99 standard but there are some limitations associated with it. You cannot have an variable length array with static or extern storage class specifier.

您有一个带有 static 存储规范的 VLA,而 C99 标准不允许这样做.

You have an VLA with static storage specification and it is not allowed by the C99 Standard.

参考:

c99 标准:6.7.5.2/8

c99 Standard: 6.7.5.2/8

示例 4 可变修改 (VM) 类型的所有声明必须在块作用域或函数原型作用域.使用静态或外部存储类说明符声明的数组对象不能具有可变长度数组 (VLA) 类型.但是,使用静态存储类说明符声明的对象可以具有 VM 类型(即指向 VLA 类型的指针).最后,所有用 VM 类型声明的标识符都必须是普通标识符,因此不能是结构或联合的成员.

EXAMPLE 4 All declarations of variably modified (VM) types have to be at either block scope or function prototype scope. Array objects declared with the static or extern storage class specifier cannot have a variable length array (VLA) type. However, an object declared with the static storage class specifier can have a VM type (that is, a pointer to a VLA type). Finally, all identifiers declared with a VM type have to be ordinary identifiers and cannot, therefore, be members of structures or unions.

因此,如果您想要一个带有 static 存储说明符的动态大小数组,则必须使用在堆上分配的动态数组.

So if you want a dynamic size array with static storage specifier you will have to use a dynamic array allocated on heap.

#define MAX_SIZE 256
static int* gArr;
gArr = malloc(MAX_SIZE * sizeof(int));


回答您更新的问题:
当您从声明中删除 static 关键字时,声明数组的存储说明符从 static 变为 global,注意上面的标准引用,它清楚地提到了 VLA 的限制staticextern 存储规范不允许.显然,您可以拥有一个具有全局存储规范的 VLA,这是您删除 static 关键字后所拥有的.


To answer your updated question:
When you remove the static keyword from the declaration, the storage specifier of the declared array changes from static to global, note the standard quote above, it clearly mentions the restriction that VLAs are not allowed with static and extern storage specification. Clearly, you are allowed to have an VLA with global storage specification, which is what you have once you remove the static keyword.

这篇关于为什么不能使静态数组的大小可变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆