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

时间:2011-07-28 06:54:39

标签: c++ alignment

假设我有一个类,它或它的成员没有明显的问题,但是如果我尝试将几个类成员的地址传递给同一模块中的另一个类,则第一个参数正确传递,但是第二个总是 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)));

1 个答案:

答案 0 :(得分:0)

Blasted GCC ......:doubleFacePalm我无法正常解决这个问题,但是我找到了一个解决方法,将所有类构造函数数据传递到一个由16个字节边界对齐的结构中。

相关问题