gpt4 book ai didi

c++ - 如何使用大 vector 初始化来避免 "compiler limit: compiler stack overflow"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:44:04 25 4
gpt4 key购买 nike

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

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

这是由一些生成的 header 引起的,其中包含:

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

如何在不使 MSVC 崩溃的情况下以这种方式使用 vector ?请注意,代码在 clang 和 gcc 中构建正常。

最佳答案

尝试将您的数据放入一个常量静态数组,然后使用 vector 的范围构造器:

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 的问题。

关于c++ - 如何使用大 vector 初始化来避免 "compiler limit: compiler stack overflow"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31028941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com