mrh*_*nia 5 c++ g++ clang segmentation-fault
我想出的问题的最小例子如下:
struct __attribute__((aligned(16))) Foo {
float x, y, z;
Foo(float x, float y, float z) : x(x), y(y), z(z) {}
};
class Bar {
public:
Foo foo;
Bar(const Foo &foo) : foo(foo) {}
Foo bar() { return foo; }
};
int main()
{
Bar *bar = new Bar(Foo(0.0f, 0.0f, 0.0f));
bar->bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果使用clang++(版本3.4,Ubuntu 14.04中可用的默认版本)进行编译,这段代码会在运行时导致分段错误.使用g++(版本4.8.4)编译时不会发生此问题.这是编译器错误还是我的代码有问题?
作为旁注:如果bar是堆栈分配,程序不会崩溃,即:
Bar bar(Foo(0.0f, 0.0f, 0.0f));
bar.bar();
Run Code Online (Sandbox Code Playgroud)
按预期工作.
小智 0
我猜想基于堆栈的版本会幸运地对齐。在指令之前添加 U8 可能会使堆栈不同,因此您可以对其进行测试。当您将其放在堆上时,它会变得随机:-) 您有可能在代码生成中遇到错误,因为您的 32 位值可能在 16 位边界上对齐,并且已为未对齐的访问生成了代码。X86 会默默地处理此异常,但不会处理浮点数。这也值得尝试 - 更改为整数。
| 归档时间: |
|
| 查看次数: |
179 次 |
| 最近记录: |