Segmentation error after alignment return

The smallest instance of my problem that I encountered is as follows:

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; } 

This piece of code leads to a segmentation error at startup if compiled with clang++ (version 3.4, available by default on Ubuntu 14.04). The problem does not occur when I compile this with g++ (version 4.8.4). Is this a compiler error or is there some problem with my code?

As a side note: the program does not crash if bar allocated on the stack, that is:

 Bar bar(Foo(0.0f, 0.0f, 0.0f)); bar.bar(); 

works as intended.

+5
source share
1 answer

I guess the stack based version is aligned in the order of luck. Adding U8 to the command could make the stack different so you can test it. When you put it on a heap, it becomes random :-) There is a chance that you have an error in generating the code. Your 32-bit values ​​are probably aligned on 16-bit boundaries, and the code was created for uneven access. X86 will silently handle this exception, but not for float. It is also worth a try - change to ints.

0
source

Source: https://habr.com/ru/post/1238973/


All Articles