| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"E G" |
| Date: |
24 Feb 2004 05:11:46 PM |
| Object: |
Question about pointer to class that has dynamic memory |
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[i]=_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!
.
|
|
| User: "David Harmon" |
|
| Title: Re: Question about pointer to class that has dynamic memory |
24 Feb 2004 06:05:06 PM |
|
|
On Tue, 24 Feb 2004 15:11:46 -0800 in comp.lang.c++, E G
<egarduno@ucsd.edu> was alleged to have written:
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".
.
|
|
|
|
| User: "Julie J." |
|
| Title: Re: Question about pointer to class that has dynamic memory |
24 Feb 2004 05:40:34 PM |
|
|
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 G wrote:
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[i]=_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!
.
|
|
|
|

|
Related Articles |
|
|