The problem arises before you begin to dynamically select objects. If you run the program with the debugger attached, you will see that the program terminates because of. Why?
Obj* ptr[1000000];
You cannot declare such a large object with automatic storage time. When main is entered, it tries to allocate space on the stack for this object and cannot do this, resulting in an excess structured stack exception. Your application does not handle this exception, so the runtime terminates the program.
Note, however, that the Obj destructor will never be called by your program. When you dynamically allocate an object using new , you are responsible for destroying it with delete . Since you never call delete to destroy the objects you created, they are never destroyed.
If you used, say, std::vector<std::unique_ptr<Obj>> instead (or, for that matter, just std::vector<Obj> ), you will see that the destructor will be called for each completely created object Obj .
source share