5

I was wondering if someone could shed some light on the deallocation-of-memory processes in c++.

If I have a struct that I declare static, such that it's constructor is the first thing to execute and its destructor is the last thing to execute:

struct initializer execute_before_and_after_main {
     initializer() { init(); }
     ~initializer() { cleanup(); }
}
static initializer execute_around_main;

And I then have something like:

class my_class {
    my_object objects[100];
}
extern my_class gobal_my_class;
my_class global_my_class;

and main is not important here:

int main (int argc, char* argv[]) {
    ....
} 

When cleanup() is called, is the objects array now containing deallocated/invalid memory? Is there a standard sequence of initialisation/destruction that c++ implements here that someone could perhaps point me to?

Thanks

EDIT: I understand this type of code is possibly not the best practice, but I am still wondering if the behaviour is defined.

ricky116
  • 744
  • 8
  • 21
  • 5
    Time to get familiar with [storage duration](http://en.cppreference.com/w/cpp/language/storage_duration) – Captain Obvlious Aug 02 '13 at 19:56
  • @CaptainObvlious lol I was about to put that same link in my answer – aaronman Aug 02 '13 at 19:56
  • 1
    There is no standard defined sequence for initialization and destruction of objects with static storage duration; this is referred to as the [static initialization order fiasco](http://www.parashift.com/c++-faq/static-init-order.html) – Praetorian Aug 02 '13 at 19:57
  • @Praetorian: Yes there is, partially. – Mooing Duck Aug 02 '13 at 19:58
  • @MooingDuck Are you referring to the fact that if they're defined in the same TU they will be initialized in the order of declaration? If so, is that really mentioned somewhere in the standard? It could just be one of those things that every implementation does, but is not mandated ... I don't know ... – Praetorian Aug 02 '13 at 20:03
  • @Praetorian: `§ 3.6.2 Initialization of non-local variables [basic.start.init]` is ~3 pages long – Mooing Duck Aug 02 '13 at 20:07
  • @Praetorian Yes, that is mandated. – jrok Aug 02 '13 at 20:11
  • @MooingDuck From § 3.6.2/2 *Variables with ordered initialization defined within a single translation unit shall be initialized in the order of their definitions in the translation unit.* Thank you! – Praetorian Aug 02 '13 at 20:12

1 Answers1

3

Static and global variables both have static storage duration meaning they are freed when the program ends. From reading one of the duplicates I found that if the code is all in the same translation unit (which yours is) then objects with static storage are destructed in the reverse order of construction. If the objects are in different translation units you can't guarantee anything.

When cleanup() is called, is the objects array now containing deallocated/invalid memory?

Yes but it doesn't really matter since it is only called once the object is out of scope

Is global_my_class destructed before execute_around_main?

yes, global my class is destructed first because it is initialized last

aaronman
  • 18,343
  • 7
  • 63
  • 78
  • "Is there a standard sequence of initialisation/destruction that c++ implements here" The fact that they're both freed when the program ends does NOT address the question of if `global_my_class`'s memory is valid during the destruction of `execute_around_main`. – Mooing Duck Aug 02 '13 at 19:58
  • @MooingDuck so if I only answer half the question you feel the need to downvote, I will try to fix the answer but I'm not 100% clear on what he's trying to ask – aaronman Aug 02 '13 at 20:07
  • "When cleanup() is called, is the objects array now containing deallocated/invalid memory?" -> "Is `global_my_class` destructed before `execute_around_main`?" – Mooing Duck Aug 02 '13 at 20:09
  • @MooingDuck the second part is tricky to answer because he didn't write it as one continuous file but I would assume that global_my_class is destructed first since it is constructed after – aaronman Aug 02 '13 at 20:12
  • @MooingDuck I tried to address all your concerns feel free to correct me – aaronman Aug 02 '13 at 20:15