如何避免“编译器限制:编译器堆栈溢出”与大矢量inits? [英] How to avoid "compiler limit: compiler stack overflow" with large vector inits?

查看:1153
本文介绍了如何避免“编译器限制:编译器堆栈溢出”与大矢量inits?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的单元测试中,我得到以下编译器错误:

In my unit tests I get the following compiler error:

 The error message indicates as follows: 
 'fatal error C1063: compiler limit: compiler stack overflow'

这是由一些生成的标题包含:

This is caused by some generated headers which contain:

std::vector<unsigned char> GetTestData()
{
     return { 0x1, 0x2, 0x3 }; // Very large 500kb of data
}

崩溃MSVC?注意,代码在clang和gcc中构建OK。

How can I use vectors in this way without crashing MSVC? Note that the code builds OK in clang and gcc.

推荐答案

尝试将数据放入一个const静态数组,然后使用向量的范围ctor:

Try putting your data into a const static array and then use vector's range ctor:

std::vector<unsigned char> GetTestData()
{
    static const unsigned char data[] = { 
        0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0,
             ...
    }; // Very large 500kb of data

    return std::vector<unsigned char>(data, data + sizeof(data));
}

编辑:感谢Lundin指出const。

Thanks Lundin for pointing out about const.

这篇关于如何避免“编译器限制:编译器堆栈溢出”与大矢量inits?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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