Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wrote the program just to check what happens if we delete this pointer in the destructor of the classs. Here is my code
class xxx{
public: xxx(){cout<<"constructor called"<<endl;}
~xxx(){cout<<"destructor called"<<endl;delete(this);} //deleting the 'this' pointer
};
int main(int argc, char *argv[])
{
xxx *x1=new xxx();
delete x1;
return 0;
}
When I run this program it results in indefinite loop printing "desctructor called".
What is the relation between delete operator and destructor function? Also what happens when you use delete this in the destructor?
this->~destructor is called, then delete(this) will will again call this->~destructor thus results in calling function indefinitely.
Of course you get an infinite loop (or perhaps a stack overflow). delete calls the destructor before deallocating the memory, so if that in turn tries to delete the object that's already being deleted, then you're in a recursive death spiral.
The simple solution is: don't do that.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Let's say I have an array a which holds pointers to objects of a class B. Let's say a[0] points to object c.
If I now set a[0] = nullptr, will the destructor of c be called and thus c destroyed if c was constructed on the stack and if a[0] was the only thing referencing c.
If you refer to regular pointers (Obj* ptr) so c's d'tor would not be called because of the assignment. It might be called if there's a code like -
... // Some code and initialization before
{
Obj c;
a[0] = &c;
a[0] = NULL; //c isn't affected in any way by this assignment
} //End of scope. **c's d'tor is called here**
Although, if c was allocated on the heap, it's memory would have leaked
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I know if I create a pointer in the header file, I should always delete it when the destructor is being called, but what about if I create a pointer inside of a function. I know basic variables get destroyed at the end of the block, is it the same for pointers?
For example:
Class::Function()
{
int i = 3; // This gets destroyed after the function ends
int* j = 5; // What about this? Do I have to delete it somewhere to keep from a leak?
}
If I initialize j inside of the constructor, I would say delete j; to prevent leaks, etc. Is there something I should do in this case?
Assigning int value to pointer
int* j = 5;
is illegal because you are storing int to int*. Anyway you can cast it
int* j = reinterpret_cast<int*>( 5 );
but dereferencing this pointer would lead to undefined behavior, since you dont know where does that pointer point.
You should init pointers like that
int* j = nullptr;
Since c++11 you cant create instance of nullptr_t and assign it.
nullptr_t initPointer;
int* j = initPointer;
If you dont use new operator to assign memory to pointer, you can't delete this pointer, it would lead to undefined behavior. Otherwise if you use new you need matching delete or you would get memory leak. If you want to check your program have memory leaks, check this thread and choose one tool. I can recommend valgrind.
Every call to new needs a matching call to delete. If you have more new's than delete's, you get memory leaks. If the opposite, you get double deletes. In your case, if you never called new, then there's no need for delete!
There are tools out there to help match your new's and deletes. My personal favorite is cppcheck. It's super quick and easy to use, and it runs on the c++ source code! It generally does a good job at catching unmatched new and delete calls.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
comming from other programming language why can't I do this in c++:
myClass mc = new myClass();
it seems that it suffices to just write:
myClass mc;
but then what if I want to use make mc be a new instance of myClass and dump the existing one?
comming from other programming language why can't I do this in c++:
Because in C++ to initilize variable types on the left and right side of = must be compatible. In your case:
myClass mc = new myClass();
mc has type myClass and expression on the right has type myClass *. So you either need to change left side to:
myClass *mc = new myClass();
or right side to:
myClass mc = myClass();
to make both sides compatible. What implications it would have on class instance lifetime you should get from a C++ textbook.
new generates the object using dynamic memory from the heap. Your second line allocates it on the stack or in the data section. Please read about the differences of stack, heap and data section and the consequences for your application!
The new keyword means allocating heap memory to a pointer
int* pointer = new int(5);
This means that the int will be allocated on the heap as opposed to the stack.
When allocating memory with the new keyword you also have to delete it
delete pointer;
In your case, you should be able to assign a new value like so
mc = myClass();
without the new keyword.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the following code:
struct A {
int someInt;
};
A *a = new A;
A *b;
delete a; // ok
delete b; // crash
It crashes, but I don't know why. I thought deleting null pointers is a no-op.
Do I even need to delete structs? Is it bad practice to create structs on the heap? Is it better to create them on the stack?
Deleting null pointers is a no-op, yes, so if b were a null pointer then you'd be fine.
But it's not. It's an uninitialised variable with an indeterminate value.
Here's a null pointer:
A* b = nullptr;
(If you write A* b; at global scope, or if you write static A* b; in a function, or if you are defining a static class member, then b has static storage duration and is automatically zero-initialised for you; this is the one exception to the rule.)
And, yes, you should avoid dynamic allocation where possible — using it for no good reason complicates your code for, well, for no good reason.
b is a uninitialized pointer. There is nothing to delete.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have created a class with one constructor and destructor. In main function, I have created a pointer and initialize to my class with "new" keyword.
#include <iostream>
using namespace std;
class RAVI
{
public:
RAVI()
{
cout<<"in constructor"<<endl;
}
~RAVI()
{
cout<<"in Distructor"<<endl;
}
};
int main()
{
RAVI *p;
try{
p= new RAVI();
cout<<"throughng object"<<endl;
throw 6;
}
catch(...)
{
cout<<"Caught exception"<<endl;
}
cout<<"end of try-catch block"<<endl;
return 0;
}
This give output:
in constructor
throughng object
Caught exception
end of try-catch block
Usually destructor should call before throwing the exception. But in the below case why it is not happening??
Please clarify my doubt?
Usually destructor should call before throwing the exception
WRONG. The object is allocated in heap, and will reside in memory until program terminates. If you used stack to create the object inside the try block, then it would get destroyed when that block ends, but still after the throw statement.
try{
RAVI p;
cout<<"throughng object"<<endl; throw 6; }
catch(...)
{
cout<<"Caught exception"<<endl;
}
Output:
in constructor
throughng object
in Distructor
Caught exception
end of try-catch block
I guess you assumed "Usually destructor should call before throwing the exception " from the knowledge that if an exception is thrown, stack will unwind and destroy all the objects created in that scope . But this is true for objects (including pointers, but not the pointed objects) that reside in stack, not in heap. That's the motivation of using smart pointer instead of raw pointer, so that when stack unwinds, the heap objects will be also destroyed.
The destructor of all local variables leaving scope will be
called. In your case, there are no local variables leaving
scope, so no destructors will be called. If RAVI* p; were in
the try block, its destructor would be called. But the
destructor of a pointer is a no-op, so you can't tell whether it
was called or not.
When you create an object with new, you're telling the
compiler that you want to manage its lifetime. The only time
its destructor will be called is when you tell the compiler to
do so, with delete.