views:

75

answers:

1

Having some really weird problem and as beginner with c++ I don't know why.

struct DeviceSettings
{
public:
....somevariables
    DXSize BackbufferSize;

....somemethods
};

struct DXPoint;
typedef DXPoint DXSize;

__declspec(align(16)) struct DXPoint
{
public:
    union
    {
        struct
        {
            int x;
            int y;
        };
        struct
        {
            int width;
            int height;
        };
        int dataint[2];
        __m128i m;
    };

    DXPoint(void);
    DXPoint(int x, int y);
    ~DXPoint(void);

    void operator = (const DXPoint& v);
};

For some reason when i declare a DeviceSettings the app crash cause the DXSize var is not aligned correctly.

But this only if compiled on 32 bit mode. Works fine in 64 bit mode...

Any clues? Am i missing something obvious?

+2  A: 

The align declspec only guarantees that the __m128i is aligned relative to the start of the data structure. If your memory allocator creates objects that aren't 16-byte aligned in the first place, the __m128i will be carefully misaligned. Many modern memory allocators give only 8-byte alignment.

You'll need to overload operator new for DXPoint to use an allocator with better alignment control, or use statically allocated and correctly aligned __m128is, or find some other solution.

--

Sorry, overlooked the "C++ beginner" part of your question. operator new overloading and custom memory allocators aren't really C++ beginner topics. If your application is such that you can allocate your DXPoint/DXSize objects statically (i.e. as globals instead of with 'new'), then that might also work. Otherwise you're diving in the pool at the deep end.

Russell Borogove
resolved by aligning all classes that use a SSE class not in pointer mode. :P
feal87