Question about pointer to class that has dynamic memory

E

E G

Hi,

I have a class similar to this:

class Matrix
{
private:
float **_A;
unsigned _rows,_cols;
public:
Matrix():_A(0),_rows(0),_cols(0){}

Matrix(unsigned const rows,unsigned const cols):_A(0),_rows(0),_cols(0)
{
if(rows>0 && cols>0){
_A=new float*[rows];
if(_A==0)
throw "Not Enough Memory";
_A[0]=new float[rows*cols];
if(_A[0]==0){
delete[] _A;
_A=0;
throw "Not Enough Memory";
}
if(rows>1)
for(unsigned i=1;i<rows;i++)
_A=_A[0]+ i*cols;
_rows=rows;
_cols=cols;
memset(_A[0],0,rows*cols*sizeof(float));
}
}

~Matrix()
{
if(_A != 0){
if(_A[0] != 0)
delete [] _A[0];
delete [] _A;
}
_A=0;
_rows=0;
_cols=0;
}
}

when I run a program similar to the following:

int main( int argc, char **argv )
{
Matrix *pfM;
{
Matrix fM(4,3);
pfM=&fM;
}
delete pfM;

return 0;
}
the following happens:
int main( int argc, char **argv )
{
Matrix *pfM;
{
Matrix fM(4,3); <------- fM._A has some assigned address as expected.
 
J

Julie J.

The destructor is not being called as a consequence of the assignment:
pfM=&fM; <------- The destructor to Matrix is called for _A and

But because of the end of the scope for fM:

Second, you can't call
delete pfM; <-------- A SIGSEGV is issued and a Segmentation fault

because pfM points to a stack-allocated variable. Doing so results in
undefined behavior (or a fault in your case). delete can only be used for
objects created in the free store through new.

E said:
Hi,

I have a class similar to this:

class Matrix
{
private:
float **_A;
unsigned _rows,_cols;
public:
Matrix():_A(0),_rows(0),_cols(0){}

Matrix(unsigned const rows,unsigned const cols):_A(0),_rows(0),_cols(0)
{
if(rows>0 && cols>0){
_A=new float*[rows];
if(_A==0)
throw "Not Enough Memory";
_A[0]=new float[rows*cols];
if(_A[0]==0){
delete[] _A;
_A=0;
throw "Not Enough Memory";
}
if(rows>1)
for(unsigned i=1;i<rows;i++)
_A=_A[0]+ i*cols;
_rows=rows;
_cols=cols;
memset(_A[0],0,rows*cols*sizeof(float));
}
}

~Matrix()
{
if(_A != 0){
if(_A[0] != 0)
delete [] _A[0];
delete [] _A;
}
_A=0;
_rows=0;
_cols=0;
}
}

when I run a program similar to the following:

int main( int argc, char **argv )
{
Matrix *pfM;
{
Matrix fM(4,3);
pfM=&fM;
}
delete pfM;

return 0;
}
the following happens:
int main( int argc, char **argv )
{
Matrix *pfM;
{
Matrix fM(4,3); <------- fM._A has some assigned address as expected.
.
.
operations with fM
.
.
pfM=&fM; <------- The destructor to Matrix is called for _A and
pfM->_A=0x0
}
delete pfM; <-------- A SIGSEGV is issued and a Segmentation fault
is called

return 0;
}

Ignoring the usefulness of this code (I reduced the code so it shows the
problem I am interested only) does anybody know a "good" method to
prevent the destructor to be called in pfM=&fM and therefore mantain the
allocated fM. I know that calling pfM=new Matrix(4,3) and the *pfM=fM
would solve the problem, but at some point I would have two objects
Matrix, I would prefer to avoid that. I also know that avoiding the
creation of the object fM and working directly with pfM would solve my
problem, but I was wondering if there was another "straight" solution to
this problem.

Thanks!
 
D

David Harmon

Matrix, I would prefer to avoid that. I also know that avoiding the
creation of the object fM and working directly with pfM would solve my
problem, but I was wondering if there was another "straight" solution to
this problem.

This is actually very, very simple. There are three kinds of memory
allocation in C++:
Static - Deallocated when your program ends.
Dynamic - Deallocated when you say so (with 'delete')
Automatic - Deallocated when it goes out of scope.

Decide which one you want when you create the object, because after that
it's too late to change your mind.

IOW, "no".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top