C++ 可以声明非常量大小的数组

#include<iostream>
using namespace std;

int main()
{
    uint32_t size;
    cin >> size;
    int arr[size];
    printf("size address::%x\n", &size);
    printf("array address::%x\n", &arr[0]);
    return 0;
}

这段代码,可以编译通过。可以在栈上开辟一个运行时才知道大小的数组。(参考:http://ericwang.github.com/c_cpp/2010/02/10/c_Variable_length_arrays.html)

可能是我太孤陋了,竟然不知道什么时候G++已经支持这个特性了。

我的机器输入1,程序输出是:

1
size address::bffff54c
array address::bffff520


posted @ 2012-08-27 19:27  junfeng_feng  阅读(640)  评论(0编辑  收藏  举报