Question about pointer to class that has dynamic memory



 DEVELOP > c-Plus-Plus > Question about pointer to class that has dynamic memory

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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!

.


  Page 1 of 1

1

 


Related Articles
Canceling file or directory deletion in .NET. That's Right Tony. How Dare You Ask a Question About .NET in This Dictatorship?
Code question that involves namespaces, templates and classes
Unix command that can post question to a group of expert like using the newsgroup.
String or char[] - that is the question
hylafax question--- how to distingish faxes belong to A or B or C ......Z if the same faxes no is employed so that I can email to inform to read their fax via internet ....
To inherit or not, that is the question
newbie question: Compiler relocation ??? What is that?
Does my object exist? So why its HWND doesn't exist? That's a question... (CMonthCalCtrl control)
Re: verify that a file exist!
Curiously recursive pattern-what is that???
Specialisation of a template that results to another template
Re: Java's performance far better that optimized C++
Operators that cannot be Overloaded - WHY?
How do I write a function that accepts a 'printf' type parameter?
is this possible - pointer to a classtype, that differs?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER