如何避免大矢量初始化"compiler limit: compiler stack overflow"?
How to avoid "compiler limit: compiler stack overflow" with large vector inits?
在我的单元测试中,我得到了以下编译器错误:
The error message indicates as follows:
'fatal error C1063: compiler limit: compiler stack overflow'
这是由一些生成的标头引起的,这些标头包含:
std::vector<unsigned char> GetTestData()
{
return { 0x1, 0x2, 0x3 }; // Very large 500kb of data
}
如何在不破坏MSVC的情况下以这种方式使用矢量?请注意,代码在clang和gcc中构建正常。
尝试将数据放入const静态数组,然后使用向量的范围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。
尝试构建一个大数组进行初始化,而不是直接使用初始化器。
std::vector<unsigned char> GetTestData()
{
static const unsigned char init[] = { 0x1, 0x2, 0x3 }; // Very large 500kb of data
return std::vector<unsigned char>(std::begin(init), std::end(init));
}
即使它在clang和gcc中构建良好,我也不建议按值返回这样的大向量。如果您正在处理的数据是不可变的,我会将其作为常量引用返回,如:
// EDIT Moved vector outside of function
static const std::vector<unsigned char> static_v({ 0x1, 0x2, 0x3 });
const std::vector<unsigned char> & GetTestData()
{
return static_v;
}
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- std::stack 是连续的吗?
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- Incomings Call with Android Sip stack in Embarcadero C++ bui
- 堆叠协程 + gdb = "previous frame inner to this frame (corrupt stack)?"
- 生成质数的程序,错误:"Stack overflow"
- How to recover system gcc compiler on centos 6
- 如何在 x64 上"stack oveflow"例外
- 为什么此代码中显示"*** stack smashing detected ***: <unknown> terminated Aborted (core dumped) "错误?
- Qt Creator 在执行步骤 "make" 时出现编译错误,-fno-stack-limit
- 无法在基于 DFS 的任务排序程序中填充"stack"
- 如何解决g++问题"internal compiler error: Illegal instruction min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MI
- std::stack的奇怪行为,pop()返回相同的值
- 从 MSVC14 切换到 MSVC16 会导致"compiler is out of heap space (C1060)"错误
- 庞大的初始化列表,如何修复"fatal error C1060: compiler is out of heap space"
- 使用双LL在C++中实现Stack失败,出现异常"EXC_BAD_ACCESS(代码=2,地址=0x7fff5
- 在析构函数中调用"delete"运算符时"compiler is out of heap space"编译器错误
- 如何获得"基于堆栈"的std::stack?(具有固定的最大尺寸)
- 为什么我会收到"invalid conversion from 'Queue*/Stack*' to 'int'"错误消息?
- 如何避免大矢量初始化"compiler limit: compiler stack overflow"?