Initialising global object arrays that are used "extern"

  • Thread starter Christoph Conrad
  • Start date
C

Christoph Conrad

Hi,

given the following test case, with CString from Microsofts MFC:

==================================================
file 1:

CString arr[100];

==================================================
file 2:

extern CString arr[];


void somefunc()
{
arr[0] = "foo";
arr[1] = "bar";
}
==================================================

It does not seem to be guaranteed that arr is initialised correctly
before usage. Is there some trick to force this?

Best regards,
Christoph
 
V

Victor Bazarov

Christoph said:
Hi,

given the following test case, with CString from Microsofts MFC:

==================================================
file 1:

CString arr[100];

==================================================
file 2:

extern CString arr[];


void somefunc()
{
arr[0] = "foo";
arr[1] = "bar";
}
==================================================

It does not seem to be guaranteed that arr is initialised correctly
before usage. Is there some trick to force this?

Unless 'somefunc' is called from a static object's constructor, you
don't have a problem. Objects with static storage duration (and the
global objects like your 'arr' have static storage duration) are
constructed before 'main' is called, and your 'somefunc' is supposedly
called after 'main' (probably while 'main' is executing).

If, in fact, 'somefunc' *is* called from another static object's c-tor,
then you may have a problem usually called "static object initialisation
order fiasco". To overcome that you might introduce a flag of sorts.
We know that initialisation within the same module happens in the order
of declarations, so if you define a static bool right after 'arr' in
the same module where 'arr' is defined, and make it *dynamically*
initialised (by calling a function) to 'true', you can then check the
value of that bool in the other module and then see if it's been already
set to 'true' (it is set to 'false' before any initialisation). Do not
use the 'arr' if the flag is 'false'... In order to have 'arr' ready
when some other static object is being constructed, you need to place
the other object in the same module as 'arr', after 'arr'.

Another way is not to have 'arr' exposed, but instead have a function
that would return elements of 'arr', like so

CString& arrAt(size_t i) {
static CString arr[100];
assert(i < 100);
return arr;
}

and use it instead of indexing the array itself. That ensures that
the array is initialised upon the first call to the function.

V
 
C

Christoph Conrad

Hi Victor,

thank you for your detailed answer. So it must be another problem in our
program.

Best regards,
Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top