Why is a POD in a struct zero-initialized by an implicit constructor when creating an object in the heap or a temporary object in the stack?
The standard and the C++ book say that the default constructor for class type members is called by the implicit generated default constructor, but built-in types are not initialized. However, in this test program I get unexpected results when allocating an object in the heap or when using a temporary object: #include<iostream> struct Container { int n; }; int main() { Container c; std::cout << "[STACK] Num: " << c.n << std::endl; Container *pc = new Container(); std::cout << "[HEAP] Num: " << pc->n << std::endl; delete pc; Container tc = Container(); std::cout << "[TEMP] Num: " << tc.n << std: