home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,810 of 33,346   
   DeMarcus to Martin Bonner   
   Re: When is the non-local static variabl   
   17 Jan 12 12:08:32   
   
   df21e0fb   
   From: use_my_alias_here@hotmail.com   
      
   On 2012-01-16 22:07, Martin Bonner wrote:   
   > On Jan 15, 11:15 pm, DeMarcus  wrote:   
   >> §3.6.2/4 says "It is implementation-defined whether the dynamic   
   >> initialization of a non-local variable with static storage duration is   
   >> done before the first statement of main.   
   >   
   >> If the initialization is   
   >> deferred to some point in time after the first statement of main, it   
   >> shall occur before the first odr-use (3.2) of any function or variable   
   >> defined in the same translation unit as the variable to be initialized."   
   >   
   >> Let's say I have this program.   
   >>   
   >> #include "SomeClass.hpp"   
   >>   
   >> SomeClass sc;   
   >>   
   >> int main()   
   >> {   
   >>     // Here we run a lot of code without accessing sc.   
   >>     // [...]   
   >>   
   >>     std::cout<<  "Shutting down"<<  std::endl;   
   >>   
   >>     sc.someFunction();   
   >>   
   >>     return 0;   
   >>   
   >> }   
   >>   
   >> As I interpret §3.6.2/1 it means the constructor of sc should be run at   
   >> "some" point in the start-up of the application.   
   > Yup.   
   >>   
   >> But as I interpret §3.6.2/4 it means that the compiler is free to run   
   >> the constructor more or less whenever it wants but before the   
   >> application shuts down.   
   >   
   > In this particular case, sc can either be initialized before main is   
   > run, (§3.6.2/1), or before any of the functions in your file are run   
   > (§3.6.2/4).  Given that the only function in your file is main, this   
   > is the same thing.   
   >   
   >   
   > if you have:   
   >   
   > file 1:   
   >    extern void main2();   
   >   int main() { main2(); return 0;}   
   >   
   > file 2: - as your example except replace main with main2   
   >   
   > Then sc can either be initialized before main is run, or after main   
   > starts, but before main2 starts executing.   
      
   Thanks!   
      
   Yes, but the problem is that I may have files where nothing is   
   referenced. The only thing in the file is a non-local static variable   
   where I want the constructor to be run.   
      
   This is fully legal with §3.7.1/2 that says "If a variable with static   
   storage duration has initialization or a destructor with side effects,   
   it shall not be eliminated even if it appears to be unused, ...".   
      
   The problem is that I have no idea when this constructor is run.   
      
   > (One can imagine an OS   
   > which dynamically loads object files into memory when a function in   
   > the object file is called.  As it does so, it performs the required   
   > dynamic initialization for static objects in the file.  Such an OS   
   > would be compatible with the standard.)   
   >   
      
   I start to think in those terms actually, having a dynamic library   
   loader that starts up the first thing that happens and then have   
   everything else loaded dynamically. But I guess there still can be   
   different translation units even in a dynamic library, right? If so, I   
   would still be stuck with my problem.   
      
      
   /Daniel   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca