[ create a new paste ] login | about

Link: http://codepad.org/tBficgxh    [ raw code | output | fork ]

C++, pasted on Apr 9:
class C2{};
class C1 {
private:
        // メンバ
        int *a ; int *b ; C2 *c ; // C2はクラスね
public:
        // コンストラクタ
        C1( void ) { a = NULL ; b = NULL ; c = NULL ; return ; } ;
        // デストラクタ
        ~C1( void ) { delete [] a ; delete [] b ; delete [] c ; return ; } ;
public:
        // 初期化メソッド
        int init( void ) {
                a = new int[ 5 ] ; b = new int[ 5 ] ; c = new C2[ 5 ] ;
                if ( !a || !b || !c ) {
                        delete [] a ; delete [] b ; delete [] c ;
                        a = NULL ; b = NULL ; c = NULL ;
                        return -1 ;
                }
                return 0 ;
        } ;
public:
        // 各種メソッド
        //int xxx( xxx ) { xxx ; } ;
} ;

#include <iostream>
int main()
{
  C1 c;
  c.init();
  {
    C1 d = c;  // c.a, c.b, c.cがd.a, d.b, d.cにコピーされる
     // d解体時 a, b, cがdeleteされる → c.a, c.b, c.cは無効ポインタ
  }
  return 0;
}


Output:
1
2
3
block freed twice

Exited: ExitFailure 127


Create a new paste based on this one


Comments: