I'm trying to understand why the destructor is called in the following code.
I guess the function f() triggers somehow the destructor, but I can't figure out why.
#include <iostream>
using namespace std;
class c {
int* p;
public:
c() { p = new int(1); }
void display() { cout << *p << endl; }
~c() { delete p; }
};
void f(c){};
void main() {
c o;
f(o);
o.display();
getchar();
}
The output is -572662307 and, at the end, this runtime exception is thrown: CrtIsValidHeapPointer(block). The destructor was called and the pointer at that specific address, deleted.