C ++:动态内存分配,复制构造函数和删除

时间:2016-11-07 19:23:27

标签: c++

由于我在代码中使用指针成员变量,因此我不得不创建动态内存分配。但当我试图删除分配的内存空间我,编译器崩溃,我不知道..为什么??

//Class definition

Class A{
private:
int max_abc; //size of array of classes p_abc
B* p_xyz; //B is some other class type
C ** p_abc; //C is some other class type
public:
A(int max_xyz =2, int max_abc =2);
A(const A & Copy); //copy constructor
~A();
}

//and my constructor is as below

A::A (int max_abc )
{
p_abc= new C*[max_abc]; //dynamic memory allocation

p_xyz= new B;
}

//copy constructor - I had to do deep copy
A:: A(const A & copy)
{
max_abc = copy.max_abc;

p_xyz= new B;
p_xyz= copy.p_xyz;

m_abc = new C*[max_abc];
for(int j=0;j<max_abc;j++)
{
m_abc [j] = new C;
m_abc [j] = copy.m_abc[j];
} 
}

//destructor definition

~A()
{
delete [] p_xyz;
for(int j=0;j<max_abc;j++)
    {
    delete m_abc [j];
    } 
    delete [] m_abc ;
}

我真的不明白,为什么我的编译器崩溃了。如果我的上述定义是正确的,或者编译器崩溃的原因,有人可以告诉我吗?请帮助....提前致谢!

0 个答案:

没有答案