析构函数调用之前从未构造过的对象

时间:2012-10-18 11:22:49

标签: c++ object-destruction

我有以下代码。

class Wave {

int m_length;
data_type * m_data;

public:

    Wave(){
        blah...blah...blah
        m_data = NULL;
        m_length = 0;
        cout << "Wave " << this << " created on " << m_data << " with m_length " <<  m_length << endl;
    }

    Wave(int len, data_type data){
        blah...blah...blah
        if (len) {
            m_length = len;
            m_data = new data_type [m_length];
        } else {
            m_length = 0;
            m_data = NULL;
        }
        cout << "Wave " << this << " created on " << m_data << " with m_length " <<  m_length << endl;
    }



    ~Wave() 
    {
    cout << "Wave " << this << " destructor  on " << m_data << " started ";
        if (m_length) delete[] m_data;
    cout << "and finished " << endl;
    };        





    Wave & operator+= (const Wave wave){
    cout << __FUNCTION__ << ":" << __LINE__ << " m_length " << m_length << endl;
    if (NULL != m_data){
        data_type * tBuf = new data_type [m_length + wave.Length()];
        copy (wave.begin(),wave.end(), copy (begin(),end(),iterator(tBuf)));
        cout << "Wave " << this << " data on " << m_data << " moved onto " << tBuf;
        delete[] m_data;
        m_data = tBuf;
        cout << " and deleted" << endl;
    } else {
        m_data = new data_type [wave.Length()];
        copy (wave.begin(), wave.end(), begin());
        cout << "Wave " << this << " data created on " << m_data << " of length " <<  wave.Length() << endl;
    }
    m_length += wave.Length();
    cout << __FUNCTION__ << ":" << __LINE__ << " m_length " << m_length << endl;
    return *this;
    };

}


main(){

blah..blah..blah

Wave sample;

for (......) {

    cout << pulseNum << "-th High part: " << pulse->first << endl;
    Wave tSample(x,y);
    blah.blah.blah

    sample += tSample;

    cout << endl << pulseNum++ << "-th Low part: " << pulse->second << endl;
    tSample = Wave(a,b);
    blah.blah.blah

    sample += tSample;
}

}

以下是执行此代码的日志

Wave 0x28fe34 created on 0 with m_length 0
0-th High part: 220
Wave 0x28fe54 created on 0xc60f00 with m_length 207
operator+=:211 m_length 0
Wave 0x28fe34 data created on 0xc610a8 of length 207
operator+=:230 m_length 207
Wave 0x28fe9c destructor  on 0xc60f00 started and finished

0-th Low part: 320
Wave 0x28febc created on 0xc61250 with m_length 301
Wave 0x28febc destructor  on 0xc61250 started and finished
operator+=:211 m_length 207
Wave 0x28fe34 data on 0xc610a8 moved to 0xc61250 and deleted 
operator+=:230 m_length 508
Wave 0x28fee0 destructor  on 0xc61250 started and finished

Wave 0x28fe54 destructor  on 0xc61250 started and finished

对我来说最奇怪的是析构函数被调用的次数多于构造函数。此外,它被称为之前从未构建过的对象,但是对于相同的数据地址 怎么会这样?

2 个答案:

答案 0 :(得分:8)

Wave有一个编译器生成的拷贝构造函数,你看不到输出。

例如,调用此复制构造函数来构造作为Wave & operator+= (const Wave wave)参数的对象。

答案 1 :(得分:2)

如果您想简化代码,只需定义一个std::vector<data_type> m_data数据成员,而不是使用原始指针(data_type * m_data)。

通过这种方式,编译器将自动生成正确的拷贝构造函数,复制operator =(并且还为符合C ++ 11的编译器移动语义)和析构函数(自动生成的拷贝)构造函数,复制操作符=和析构函数将按成员方式操作,例如,自动生成的复制构造函数将为类的每个数据成员调用复制构造函数。)

分配数据,而不是:

m_data = new data_type [m_length];

使用:

m_data.resize(m_length);

您还可以删除m_length数据成员,因为std::vector知道自己的大小(您可以通过std::vector::size()方法访问它)。

请注意std::vector将所有元素存储在一个连续的内存区域(如new[]);您可以使用&m_data[0]访问该区域的开头。