数组是静态的,但是直到运行时才知道数组的大小.这怎么可能? [英] The array is static, but the array size isn't know until runtime. How is this possible?

查看:125
本文介绍了数组是静态的,但是直到运行时才知道数组的大小.这怎么可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一直困扰着我一段时间.这是我(缺乏)对静态和动态内存分配之间差异的了解的核心.以下数组是一个普通的静态数组,这应该意味着在编译时分配了内存,对吗?但是,我已经进行了设置,以便用户在运行时输入数组大小.

This has been troubling me for a while. It goes to the heart of my (lack of) understanding of the difference between static and dynamic memory allocation. The following array is an ordinary static array, which should mean the memory is allocated during compile time, correct? Yet, I've set it up so that the user enters the array size at runtime.

#include <iostream>
using namespace std;

int main() {
  cout << "how many elements should the array hold? ";
  int arraySize;
  cin >> arraySize;

  int arr[arraySize];

  for (int i = 0; i < arraySize; ++i)
    arr[i] = i * 2;

  return 0;
}

请注意,此程序中没有newdelete运算符.它在Xcode 4.2(默认的Clang编译器)以及我学校的UNIX服务器(GCC 4.4.5)中都可以正常工作.在编译时创建数组时,编译器如何知道为arr分配多少内存?这只是我的编译器的fl幸,可能破坏其他内存的危险代码,还是合法的?

Note that there are no new or delete operators in this program. It works fine in Xcode 4.2 (default Clang compiler) as well as my school's UNIX server (GCC 4.4.5). How does the compiler know how much memory to allocate for arr when the array is created at compile time? Is this just a fluke of my compiler, dangerous code that could corrupt other memory, or is this legit?

推荐答案

这是C ++编译器的非标准扩展.请注意,在C语言中,与C ++语言不同,自C99以来正式支持(即标准行为).在C ++中,不支持它,因为已经有解决该问题的方法:使用std::vector而不是数组.

This is a non-standard extension of your C++ compilers. Note that in C, unlike in C++, this is officially supported (i.e. standard-mandated behaviour) since C99. In C++, it is not supported because there's already a solution to the problem: Use std::vector instead of the array.

但是不是使用静态内存分配(也不使用动态内存分配)而是自动内存分配来不是该数组.自动变量在函数末尾自动释放(分配它们的存储区称为堆栈,因为在其上的分配和释放具有堆栈语义).要使数组使用静态内存分配,您必须将static放在定义的前面(请注意,尽管全局或命名空间范围内的变量始终使用静态内存分配).但是,如果将变量设为静态,则会发现编译器不再允许使用非恒定数组大小.

Not however that the array is not using static memory allocation (nor dynamic memory allocation), but automatic memory allocation. Automatic variables are automatically deallocated at the end of the function (the memory area where they are allocated is known as the stack, because the allocations and deallocations on it have stack semantics). To have the array use static memory allocation you would have to put static in front of the definition (note that variables in global or namespace scope always use static memory allocation, though). However, if you make the variable static, you'll find that the compiler doesn't allow to use a non-constant array size any more.

请注意,std::vector会使用动态内存分配来存储其数据.因此,即使对于静态std::vector,您也可以使用非恒定大小.

Note that std::vector stores its data with dynamic memory allocations instead. For that reason, you can also use a non-constant size even for static std::vectors.

这篇关于数组是静态的,但是直到运行时才知道数组的大小.这怎么可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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