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. 2 1289
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!
On Tue, 24 Feb 2004 15:11:46 -0800 in comp.lang.c++, E G
<eg******@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". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: soni29 |
last post by:
hi,
i'm reading a c++ book and noticed that the author seems to allocate
memory differently when using classes, he writes:
(assuming a class called CBox exists, with member function size()):
//...
|
by: cppaddict |
last post by:
Hi,
I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body...
|
by: Flzw |
last post by:
Well I have a map like this :
std::map <string, CObject> ObjectList;
I have a function like this :
CObject* NewObject( char* Name, CArg* Arg)
{
std::string key = Name;
ObjectList =...
|
by: Gonçalo Rodrigues |
last post by:
Hi all,
I have a template class (call it Object) whose instances have a
variable size part - an array of of T objects. But this variable size
part is fixed at creation time so instead of...
|
by: __PPS__ |
last post by:
Hello everybody
in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"
My answer was:
"dangling pointer is a pointer that points to some...
| |
by: anonymous |
last post by:
Thanks your reply.
The article I read is from
www.hakin9.org/en/attachments/stackoverflow_en.pdf.
And you're right. I don't know it very clearly. And that's why I want
to understand it; for it's...
|
by: kathy |
last post by:
I am using std::vector in my program:
func()
{
std::vector <CMyClass *> vpMyClass;
vpMyClass.push_back(new CMyClass());
vpMyClass.push_back(new CMyClass());
vpMyClass.push_back(new...
|
by: Markus Svilans |
last post by:
Hello,
My question involves virtual functions and inheritance.
Suppose we have a class structure, that consists of "data" classes, and
"processor" classes. The data classes are derived from...
|
by: Aaron |
last post by:
I want to build two abstract base classes.
1) Game
2) Position
The Game object represents a boardgame in its entirety and the
Position object represents individual game states. The Game object...
|
by: letz |
last post by:
Hi,
We have a class whose objects are to be allocated in shared memory. To
do that a ShmManager base class is defined so that operator new and
delete are redefined to allocate segments in shared...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |