在C堆栈动态数组分配 [英] Dynamic array allocation on stack in C

查看:169
本文介绍了在C堆栈动态数组分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天刚做了一个实验,并发现一些令人困惑的:

I just did a experiment yesterday, and find something confusing:

#include <stdio.h>

int main()
{
    int j;
    scanf("%d",&j);
    const int i = j;
    int arr[i];
    return 0;
}

Ĵ从键盘读取,它的用于分配的阵列改编在堆栈中。

The number j is read from keyboard and it’s used to allocate the array arr on the stack.

编译器甚至不知道在编译时数组的大小(初始化J可0?),但没有编译错误。这怎么可能?

The compiler does not even know the size of the array at compile time (initializes j to 0?), but there is no compilation error. How is it possible?

推荐答案

变长数组分别加入C99。它在C99理由描述:

Variable length arrays were added to C99. It's described in the C99 rationale:

6.7.5.2数组声明

6.7.5.2 Array declarators

C99增加了所谓的变长数组类型的新数组类型。该
  无力声明数组的大小只在执行时知
  经常被援引作为一个主要的威慑使用C为数字
  计算语言。执行时间一些标准概念的采用
  阵列被认为是至关重要的C'S在数值验收
  计算机世界。

C99 adds a new array type called a variable length array type. The inability to declare arrays whose size is known only at execution time was often cited as a primary deterrent to using C as a numerical computing language. Adoption of some standard notion of execution time arrays was considered crucial for C’s acceptance in the numerical computing world.

元素的一个变量的声明中指定的数
  长数组类型是一个运行时前pression。 C99之前,这个尺寸
  前pression被要求为整型常量前pression。

The number of elements specified in the declaration of a variable length array type is a runtime expression. Before C99, this size expression was required to be an integer constant expression.

有没有在栈上动态数组分配。数组大小在声明中指定了。

There is no "dynamic array allocation on the stack". The array size has to be specified at the declaration.

一些编译器,如 GCC 让他们为C90的扩展(海合会,这相当于ANSI和C89)模式和C ++。在这种情况下,你会得到一个警告( -Wpedantic )或错误( -Werror -pedantic-错误)。您的编译器查阅文档。

Some compilers, like GCC allow them as an extension in C90 (in GCC, this is equivalent to ansi and C89) mode and C++. In these cases, you will get a warning (-Wpedantic) or an error (-Werror or -pedantic-errors). Consult the documentation for your compiler.

每@ Deduplicator的评论,你似乎有一种误解。变长数组不能被声明为static。

Per @Deduplicator's comment, you seem to have a misconception. Variable length arrays cannot be declared static.

第6.7.6.2

10 例4 动态可变的所有声明(VM)
  类型必须是在任块范围或函数原型范围。
  与 _Thread_local 静态声明数组对象的extern
  存储类说明不能有一个可变长度阵列(VLA)
  类型。然而,随着静态声明的对象存储类
  符可以具有一个虚拟机的类型(即,一个指向VLA类型)。
  最后,与VM类型中声明的所有标识符必须是普通
  标识符和不能,因此,是会员ostructures或联合。

10 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 _Thread_local, 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 ostructures or unions.

这意味着静态存储和自动存储是相互排斥的。

This means that static storage and automatic storage are mutually exclusive.

这篇关于在C堆栈动态数组分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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