views:

892

answers:

3

If I define a local variable instance of a class halfway down my function without using a pointer and new, does the constructor get called on entering the function or where it is defined?

If I define another instance of a class globally within the file does that constructor get called when executable is first loaded? What if multiple threads are accessing the .dll?

Finally is you answer going to be the same in .dll, .so, .exe and linux executables?

+8  A: 

If I define a local variable instance of a class halfway down my function without using a pointer and new, does the constructor get called o entering the function or where it is defined?

When it is defined.

If I define another instance of a class globally within the file does that constructor get called when executable is first loaded?

Yes.

What if multiple threads are accessing the .dll?

DLLs usually only get loaded once for the whole application – in fact, DLLs also have an entry point which is called by your application's threads but global variable initialization happens before that and only once.

Konrad Rudolph
Actually, on Windows, the DLL entry point is called for every created thread (to allow for the creation of thread local storage). Not sure about other OS's
zdan
However, global variables in the DLL are constructed only once: when the DLL is first loaded, and within the context of the thread which first loads the DLL.
ChrisW
@zdan: Right, sorry. Let me correct that. I actually wrote this wrong once before here on SO and was corrected as well.
Konrad Rudolph
+1  A: 

If I define a local variable instance of a class halfway down my function without using a pointer and new, does the constructor get called on entering the function or where it is defined?

Such variables have local scope. Their constructor is called when they're defined. For local statics, the constructor is only called once, since the statics will survive multiple function calls and returns. The order is important, and is the order of definition:

void foo() {
    ....
    if(cond) {
        ...
        // called here: first for f, then for b
        static Foo f;
        static Bar b;
    }

    ...
    Foo f; // not static: called here, in every invocation of foo.
}

If I define another instance of a class globally within the file does that constructor get called when executable is first loaded?

Yes, such variable is said to have static storage duration, and namespace scope. Its constructor is called at program start. The order is the order it is defined in the file. That is, a variable defined later will have its ctor called later. The order in which variables defined in different translation units is not defined (look-out for the static initialization order fiasco). But they are all called at program start.

What if multiple threads are accessing the .dll?

All bets are off. The variable is only constructed once. After that, when you start threads and access it, the variable has to be thread safe, or the threads has to do proper locking when accessing the variable.

Johannes Schaub - litb
I'm sorry i had the initialization of the statics wrong. Constructors for local static non-PODs are actually called when control goes through their definition - not when/before their block is entered (that is only true for PODs). It becomes important when exceptions enter the game. Cheers
Johannes Schaub - litb
are you sure it's guaranteed that bar's constructor will be called once here? void foo(){ static bar instance;}even if multiple threads reach here at the same time in the initial construction?
Idan K
it's only safe with one thread. if you have multiple threads, you have to ensure mutual exclusion so the initialization happens only once and nothing bad happens. (what i meant by the last part of my answer is that there is no such thing as thread-local-statics by default: only one static object exists and the threads will have to share that object)
Johannes Schaub - litb
A: 

For .dlls, it depends on the compiler and the C runtime (CRT). For MSVC released with Visual Studio 2008 SP1, the .dll's CRT will initialize global objects at DLL_PROCESS_ATTACH time and destroy them at DLL_PROCESS_DETACH time, assuming that nothing horrible happened during DLL_PROCESS_ATTACH.

MSN

MSN