【问题标题】:SSE Alignment with classSSE 与类对齐
【发布时间】:2011-03-14 07:45:49
【问题描述】:

遇到一些非常奇怪的问题,作为 c++ 的初学者,我不知道为什么。

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

由于某种原因,当我声明 DeviceSettings 时,应用程序崩溃导致 DXSize 变量未正确对齐。

但这只有在 32 位模式下编译。在 64 位模式下工作正常...

有什么线索吗?我错过了什么明显的东西吗?

【问题讨论】:

    标签: c++ memory crash alignment sse


    【解决方案1】:

    align declspec 仅保证 __m128i 相对于数据结构的开头对齐。如果您的内存分配器首先创建的对象不是 16 字节对齐的,那么 __m128i 将被小心地错位。许多现代内存分配器仅提供 8 字节对齐。

    您需要为 DXPoint 重载 operator new 以使用具有更好对齐控制的分配器,或者使用静态分配和正确对齐的 __m128is,或者找到其他解决方案。

    --

    抱歉,忽略了您问题的“C++ 初学者”部分。 operator new 重载和自定义内存分配器并不是真正的 C++ 初学者主题。如果您的应用程序可以静态分配您的 DXPoint/DXSize 对象(即作为全局变量而不是“新”),那么这也可能有效。否则,您将在深水池中潜水。

    【讨论】:

    • 通过对齐所有使用 SSE 类而不处于指针模式的类来解决。 :P
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多