编译器初始化杭大STD时::阵列 [英] Compiler hang when initializing large std::arrays

查看:145
本文介绍了编译器初始化杭大STD时::阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要初始化一个非常大的多维的std ::数据阵列

 类的东西;世界级
{
上市:
    世界(){空间} nullptr {};
    〜世()=默认值;
私人的:
    静态无符号整型常量的大小= 1000;
    的std ::阵列<的std ::阵列<的std ::阵列<的std ::的unique_ptr<东西>中尺寸>中尺寸>中规模>空间;
};

如果您尝试实例化这个,G ++ 4.8.2电抗器:它消耗所有可用内存,并不会返回。也就是说,编译器挂起,我从来没有得到一个可执行文件。 这是为什么?注意铛++没有麻烦。

注:我充分认识到,把堆栈上这么多的数据可能会溢出它。 什么是初始化堆的最好方法是什么?我觉得做空格引用(到分配的内存),将是最好的方式,但我想不通的语法了。


解决方案

好吧,我无法解释为什么G ++是在这个令人作呕的细微差别,但要等到工作了这一点,认为这是为你的成员声明:

<$p$p><$c$c>std::vector<std::array<std::array<std::unique_ptr<Thing>,size>,size>>空间;

和在构造函数初始化列表:

 世界(){空间大小}

这至少应该让你编译和移动这一切堆。注意:这个最好是一个64位的过程。我将不得不打猎找出为什么G ++是做什么的,我怀疑它在做什么。

I need to initialize a very large multidimensional std::array of data:

class Thing;

class World
{
public:
    World() : space{nullptr} {};
    ~World() = default;
private:
    static unsigned int const size = 1000;
    std::array<std::array<std::array<std::unique_ptr<Thing>, size>, size>, size> space;
};

If you try to instantiate this, G++ 4.8.2 chokes: it consumes all the available memory and will not return. That is, the compiler hangs and I never get an executable. Why is this? Note that clang++ has no trouble.

Note: I fully realize that putting this much data on the stack can overflow it. What is the best way to initialize it on the heap? I think making space a reference (to allocated memory) would be the best way, but I can't figure the syntax out.

解决方案

Ok, i can't explain the nuance of why g++ is puking on this, but until you work that out, consider this for your member declaration:

std::vector<std::array<std::array<std::unique_ptr<Thing>,size>,size>> space;

and in the constructor initializer list:

World() : space{size}

That should at least get you compiling and move all this to the heap. Note: this better be a 64bit process. I'm going to have to hunt to find out why g++ is doing what I suspect it is doing.

这篇关于编译器初始化杭大STD时::阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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