473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Initialising global object arrays that are used "extern"

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
Mar 1 '07 #1
2 2067
Christoph Conrad wrote:
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[i];
}

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 1 '07 #2
Hi Victor,

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

Best regards,
Christoph
Mar 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
111
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1)...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
4
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a...
5
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern"...
19
by: Karel Van Laer | last post by:
Hi everyone, I need to be able to mix C and C++. The main program is written in C and needs to access C++ code. To be more specific it's a user defined function in fluent, but this should not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.