Apple GCC 4.2.1将指针传递给构造函数

Apple GCC 4.2.1 passing a pointer to constructor

本文关键字:构造函数 指针 GCC Apple      更新时间:2023-10-16

假设我有一个类,它或它的成员没有明显的问题,但是如果我试图将几个类成员的地址传递给同一模块中的另一个类,第一个参数被正确传递,但第二个参数总是NULL!这怎么可能呢?是否存在某种隐藏的堆栈/堆损坏或某种对齐问题?MSVC没有问题,但是…

class myType2
{
     char c1;
     char c2;
} __attribute__ ((aligned (1))); // Yes, align differs and I can't change it
class MyClass2
{
public:
    MyClass2(myType1* type1, myType2* type2, int data)
    {
         // type1 and data are ok, but type2 is NULL...
    }
} __attribute__ ((aligned (16)));
class MyClass
{
public:
    myType1 type1;
    myType2 type2;
    //....
    void test()
    {
        MyClass2 test1(&this->type1, &this->type2, 170);
        MyClass2* pTest2 = new MyClass2(&this->type1, &this->type2, 170); // Same result
        myType2 localType2;
        MyClass2 test3(&this->type1, &localType2, 170); // Same result!!!
    }
} __attribute__ ((aligned (16)));

该死的GCC…我不能正常解决这个问题,但我找到了一个解决办法,通过传递所有类构造函数数据在一个16字节边界对齐的结构。