In the following code, if I include dealloc_dMAT into the destructor, it does not work. But if I call dealloc_dMAT specifically and then call the destructor, it works. Why would this be the case? The compiler I use is g++ 4.8.2 under linux.
The following code does not work,
#include <iostream>
using namespace std;
class QO{
public:
QO();
~QO();
int DIM;
double **dMAT;
void alloc_dMAT();
void dealloc_dMAT();
};
QO::QO(){
DIM=4;
cout << "DIM=" << DIM << endl;
alloc_dMAT();
}
QO::~QO(){
dealloc_dMAT();
}
void QO::alloc_dMAT(){
dMAT=new double * [DIM];
for(int i=0; i<DIM; i++) dMAT[i]=new double [DIM];
}
void QO::dealloc_dMAT(){
for(int i=0; i<DIM; i++) delete [] dMAT[i];
delete [] dMAT;
}
int main(){
QO QB;
//QB.dealloc_dMAT();
QB.~QO();
return 1;
}
But a slight modification on it works
#include <iostream>
using namespace std;
class QO{
public:
QO();
~QO();
int DIM;
double **dMAT;
void alloc_dMAT();
void dealloc_dMAT();
};
QO::QO(){
DIM=4;
cout << "DIM=" << DIM << endl;
alloc_dMAT();
}
QO::~QO(){
//dealloc_dMAT();
}
void QO::alloc_dMAT(){
dMAT=new double * [DIM];
for(int i=0; i<DIM; i++) dMAT[i]=new double [DIM];
}
void QO::dealloc_dMAT(){
for(int i=0; i<DIM; i++) delete [] dMAT[i];
delete [] dMAT;
}
int main(){
QO QB;
QB.dealloc_dMAT();
QB.~QO();
return 1;
}
EDIT: Thanks to Dave M.'s comment, it seems the code above does not fully display my issue, so here is a more accurate example of the trouble I ran into. If you compile and run it, you will notice the code abort at i=0, which should not happen if ~QO is called twice only when quitting the main.
#include <iostream>
#include <stdlib.h>
using namespace std;
class QO{
public:
QO();
~QO();
int DIM;
double **dMAT;
void alloc_dMAT();
void dealloc_dMAT();
};
QO::QO(){
DIM=4;
cout << "DIM=" << DIM << endl;
alloc_dMAT();
}
QO::~QO(){
dealloc_dMAT();
}
void QO::alloc_dMAT(){
dMAT=new double * [DIM];
for(int i=0; i<DIM; i++) dMAT[i]=new double [DIM];
}
void QO::dealloc_dMAT(){
for(int i=0; i<DIM; i++) delete [] dMAT[i];
delete [] dMAT;
}
int main(){
for(int i=0; i<10; i++){
QO QB;
QB.~QO();
cout << "i= " << i << endl;
}
system("pause");
return 1;
}