Re: mysterious destructors
ram@zedat.fu-berlin.de (Stefan Ram) wrote in news:destructors-
20150218010557@ram.dialup.fu-berlin.de:
I have written the following program:
int main()
{ c o = * new c( 1 );
o.print();
o = * new c( 2 ); /* overwrite */
o.print(); }
The program prints:
In the line marked with ?/* overwrite */?, instance #1 in the
variable ?o? is overwritten with another instance of the same
class c if I understand it correctly.
This is because you have mixed up instances with values. In your program
you have 3 instances of class c, 2 of them are leaked and so there is
only a single destructor call when the object o goes out of scope. On the
other hand, you have two values (1 and 2), copy them over across
instances and print out with misleading messages, so there is no wonder
one gets confused.
You can have an example with 2 instances (of class c) and no memory leaks
as well:
#include <iostream>
#include <ostream>
#include <memory>
struct c
{ int v;
c( int const x ): v( x )
{ ::std::cout << "constructor of instance #" << v << ".\n"; }
~c(){ ::std::cout << "destructor of instance #" << v << ".\n"; }
void print(){ ::std::cout << "I am instance #" << v << ".\n"; }};
int main()
{ std::unique_ptr<c> o(new c( 1 ));
o->print();
o = std::unique_ptr<c>(new c( 2 )); /* overwrite */
o->print(); }
This prints:
constructor of instance #1.
I am instance #1.
constructor of instance #2.
destructor of instance #1.
I am instance #2.
destructor of instance #2.
For some more fun, here is another example having 2 instances and no
memory leaks. This one is probably even more confusing ;-)
#include <iostream>
#include <ostream>
struct c
{ mutable int v;
c( int const x ): v( x )
{ ::std::cout << "constructor of instance #" << v << ".\n"; }
void operator=(const c& y) const {v = y.v;}
~c(){ ::std::cout << "destructor of instance #" << v << ".\n"; }
void print() const { ::std::cout << "I am instance #" << v << ".\n";
}};
int main()
{ const c& o = c( 1 );
o.print();
o = c( 2 ); /* overwrite */
o.print(); }
constructor of instance #1.
I am instance #1.
constructor of instance #2.
destructor of instance #2.
I am instance #2.
destructor of instance #2.