Anonymous
Anonymous

Reputation: 4197

Destructors called upon program termination

When an object is created in your main() function, is its destructor called upon program termination? I would assume so since main() still has a scope( the entire program ), but I just wanted to make sure.

Upvotes: 4

Views: 3370

Answers (2)

Hans Passant
Hans Passant

Reputation: 941208

The scope of declarations inside main() is not the entire program. It behaves just like any normal function. So, yes, destructors of local class objects execute as expected. Unless the program terminates abnormally.

Upvotes: 3

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

It depends on how your program terminates. If it terminates by having main return (either by an explicit return or falling off the end), then yes, any automatic objects in main will be destructed.

But if your program terminates by calling exit(), then main doesn't actually go out of scope and any automatic objects will not be destructed.

Upvotes: 17

Related Questions