User 10482
User 10482

Reputation: 1012

Freeing static object memory completely

Going through Modern C++ Design book, I came across this when the author talks about Singletons.

C++ guarantees that static objects memory lasts for the duration of the program

(He uses this to ensure its always possible to recreate the "Phoneix" Singleton)

Is it the case that I can delete the static object but can never release it's memory? If so, creating large static objects would mean it's memory is lost forever (till life of program ends)

Upvotes: 0

Views: 100

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 123566

If so, creating large static objects would mean it's memory is lost forever (till life of program ends)

This sounds as if it was a downside of static objects. Not sure, but maybe you missed the point. Sometimes you need an object to be alive for the whole duration of the program. How do you ensure that? You make it a static object, because...

C++ guarantees that static objects memory lasts for the duration of the program

If you don’t want the object to be alive for the whole duration of the program, then don’t make it static.

Upvotes: 4

Related Questions