What is the value of a.val after line 15? Unknown, undefined, uninitialized.
It just looks like that the different instances of 'a' on different calls of f() happen to occupy the same memory location. The destructor is called, but the default destructor does nothing that you could see (at least on your platform).
Thanks keskiverto. Yes, I forgot to initialize val in the constructor. Sorry about that.
So I added the initialization for value. Does this example have a memory leak?:
#include <iostream>
class A
{
private:
int val;
public:
A() : val(0) { }
void setVal(int v) { val = v; }
void incVal() { val++; }
int getVal() { return val; }
};
int main()
{
for (int i=0; i<3; i++)
{
A a;
a.incVal();
std::cout << "val=" << a.getVal() << std::endl;
// A destructor is automatically called because its scope of existence has finished
}
}