所以,我仍在学习按位运算,无法弄清楚为什么会发生这个错误。我已经用谷歌搜索了它,似乎在弄乱堆栈时会发生此错误,或者在某些情况下,它与 CPU 架构有关。我尝试使用不同的标志进行编译,这些标志应该有帮助,但我无法让它工作。
这是快速的代码:
int corners = 0;
for (int i = 0; i < 8; i++)
{
const ivec3 cornerPos = leaf->min + CHILD_MIN_OFFSETS[i];
const float density = Density_Func(vec3(cornerPos));
const int material = density < 0.f ? MATERIAL_SOLID : MATERIAL_AIR;
corners |= (material << i);
}
并且错误“非法指令(核心转储)”发生在该行
corners |= (material << i);
这是调试器的输出:
Signal received: SIGILL (Illegal instruction) For program, pid 26,118
我将给出这个循环的输出(它永远不会超过第一个循环)。这是 couts 的代码:
int corners = 0;
std::cout<<"corners(outside loop): "<<corners<<std::endl;
for (int i = 0; i < 8; i++)
{
const ivec3 cornerPos = leaf->min + CHILD_MIN_OFFSETS[i];
std::cout<<"cornerPos.x: "<<cornerPos.x<<std::endl;
std::cout<<"cornerPos.y: "<<cornerPos.y<<std::endl;
std::cout<<"cornerPos.z: "<<cornerPos.z<<std::endl;
const float density = Density_Func(vec3(cornerPos));
std::cout<<"density: "<<density<<std::endl;
const int material = density < 0.f ? MATERIAL_SOLID : MATERIAL_AIR;
std::cout<<"material: "<<material<<std::endl;
std::cout<<"MATERIAL_SOLID: "<<MATERIAL_SOLID<<std::endl;
std::cout<<"MATERIAL_AIR: "<<MATERIAL_AIR<<std::endl;
std::cout<<"i: "<<i<<std::endl;
corners |= (material << i);
std::cout<<"corners(inside loop): "<<corners<<std::endl;
}
这是输出:
[corners(outside loop): 0] [cornerPos.x: -32] [cornerPos.y: -32] [cornerPos.z: -32] [density: -30] [material: 1] [MATERIAL_SOLID: 1] [MATERIAL_AIR: 0] [i: 0]
我将非常感谢有人能给我关于为什么会发生这种情况的任何见解,如果有明确的原因,如何解决问题。
谢谢!