C++ default constructor, initializing pointer with new object
Solution 1
That's because you are trying to delete too much:
- you are deleting a non-allocated object in the second constructor (remove
delete yc;
) - you are trying to delete a stack-allocated object, b.
delete a;
will try to delete a pointer to b, which is an object on the stack; what happens depend on your OS (I expect an exception/core dump/whatever)
EDIT: another problem I spotted..
a->setMyClass(NULL)
I would suggest:
- this post on smart pointers
- this blog post on RAII
- any C/C++ primer explaining stack vs. heap allocation (static vs. dynamic?)
Solution 2
You're violating the rule of three.
Also this is recipe for disaster:
myClass(yourClass * tyc ) { delete yc; yc = tyc; }
What happens if tyc==yc
? Right. Not pretty :)
myClass(yourClass * tyc ) { if (yc!=tyc) { delete yc; yc = tyc; } }
Alex
Updated on July 09, 2022Comments
-
Alex almost 2 years
I have the following problem: In myClass I want to default initialize a pointer to yourClass, with a new yourClass adress. Unfortunately, if I want to delete the pointer at any point I get a (core dump).
class myClass { protected: yourClass * yc; public: myClass() { yc = new yourClass(); } myClass(yourClass * tyc ) { delete yc; yc = tyc; } ~myClass() { delete yc; yc = NULL; } void setMyClass (yourClass * tyc) { delete yc; yc = tyc; } void print () { yc->print(); } }; int main() { yourClass b (//parameter); myClass * a = new myClass(); a->print(); a->setMyClass(&b) a->print(); delete a; return 0; }
The print() of a, should result in two different prints, dependent on //parameters.
I considered yourClass yc; instead of a yourClass* yc, but I want to know if it is possible.
EDIT: I reworked the code in the following way and it works. Still looks complicated, smart pointers seem promising and I still did not apply the "Rule of Three". Here the code. Thanks all.
class myClass { protected: yourClass * yc; bool dynamic; public: myClass() { dynamic = true; yc = new yourClass (); } myClass (yourClass * tyc ) { // dynamic init (like default) if (tyc == NULL ) { dynamic = true; yc = new yourClass (); } // static use of yc else { dynamic = false; yc = tyc; } } // because only if dynamic is true, we need to erase ~blu () { if (dynamic) { delete yc; dynamic = false; } } void setMyClass(yourClass* tyc) { // leaving unchanged if new-stuff is NULL or like old-stuff if ( tyc == yc || tyc == NULL ) return; else // treating dynamic and static differently { if (dynamic) // if flag is set, must be deleted { delete yc; yc = tyc; dynamic = false; } else // must not be deleted, dynamic is still false { yc = tyc; } } } void print () { yc->print(); } };
-
Crog over 11 yearsA work around for the second issue may be to store a flag for 'ownsYc' which you could set as false when setMyClass () is called and only delete yc in the destructor if the flag is true
-
Lorenzo Dematté over 11 yearsit is more disaster than that :) in the constructor, yc will always be not initialized: if you are lucky, it's delete NULL (segfault); otherwise it's delete some-random-thing
-
Lorenzo Dematté over 11 years@Crog ...ummm... yes, but still it is a very, very dangerous design.. In C++, I believe it is always better to have RIIA; besides, there are smart pointers (stroustrup.com)
-
Lorenzo Dematté over 11 yearsSo the code you are proposing will crash anyway (assume the compiler initialize yc to 0...)
-
sehe over 11 years@dema80 I agree, I didn't have the time to actually read that code. Shame on me. I just thought I'd highlight this classic pitfall, since it is slightly more subtle (even Scott Meyer once published a smartptr with this type of bug, in a book)