Connecting Tech Pros Worldwide Help | Site Map

order of extern initialization?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:58 AM
Kyle Kolander
Guest
 
Posts: n/a
Default order of extern initialization?

I was looking over the C++ standard for this answer and didn't find it...
probably not looking in the right spots.
Here's a quick setup:

// sn.h
extern const std::string SOME_NAME;
// sn.cpp
const std::string SOME_NAME = "SomeName";
/* sn.cpp is compiled and linked into libSomeName.so */

// myClass.h
#include <sn.h>
....
std::string ary[1] = { SOME_NAME };
// myClass.cpp
....
string s1 = ary[0]; // "" -- empty string
string s2 = SOME_NAME; // "SomeName"

Is this expected behavior? What is the order of initialization of global
variables (ary) vs. an extern string defined in a source file that has
already been compiled and linked into a library? I admittedly have not
spent enough time working with extern and generally shy away from global
variables...

Thanks,
Kyle



  #2  
Old July 23rd, 2005, 04:58 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: order of extern initialization?

Kyle Kolander wrote:[color=blue]
> I was looking over the C++ standard for this answer and didn't find it...
> probably not looking in the right spots.
> Here's a quick setup:
>
> // sn.h
> extern const std::string SOME_NAME;[/color]

'SOME_NAME' has external linkage.
[color=blue]
> // sn.cpp
> const std::string SOME_NAME = "SomeName";[/color]

'SOME_NAME' has external linkage and static storage duration and
is dynamically initialised (since it's not a POD).
[color=blue]
> /* sn.cpp is compiled and linked into libSomeName.so */
>
> // myClass.h
> #include <sn.h>
> ...
> std::string ary[1] = { SOME_NAME };[/color]

'ary' has static storage duration and is dynamically initialised.
[color=blue]
> // myClass.cpp
> ...
> string s1 = ary[0]; // "" -- empty string
> string s2 = SOME_NAME; // "SomeName"[/color]

Both 's1' and 's2' have static storage duration and are dynamically
initialised.
[color=blue]
> Is this expected behavior?[/color]

Yes, just like any other. The order of initialisation of objects with
static storage duration defined in different translation units is simply
_unspecified_.
[color=blue]
> What is the order of initialization of global
> variables (ary) vs. an extern string defined in a source file that has
> already been compiled and linked into a library?[/color]

There is no such thing as "a library" in the language definition. It
is all platform-specific.
[color=blue]
> I admittedly have not
> spent enough time working with extern and generally shy away from global
> variables...[/color]

Not a bad idea.

V
  #3  
Old July 23rd, 2005, 04:58 AM
Kyle Kolander
Guest
 
Posts: n/a
Default Re: order of extern initialization?

Thanks, Victor!
I really appreciate your help on this newsgroup.
Kyle


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.