473,795 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2083
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
2651
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
6473
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) The whole "function declaration Vs object definition" fiasco, which results in the following (temporary creating) syntax: Blah poo = Blah();
9
8256
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 solution? Declaring the function extern "C" fails, because linkage declarations must be made at file scope. #include <stdlib.h> //for qsort template <class T> class Sorter
10
6200
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
12
2730
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 linkage? Not much on this in the FAQ or tutorials.
19
3861
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 storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
4
4759
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 global function with the extern "C" prefix. I was leaning toward using the static method approach until I saw a posting that said compatibility between static C++ functions and C is not guaranteed in the respective language standards. This made me...
5
1937
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" qualifier - it appears to work: x is shared between w.c and ww.c. Or if I simple don't use "extern", it works. Why? What is the correct (or simply preferred) usage? ---John PSI'm using gcc (GCC) 4.0.2.
19
391
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 be relevant. I've allready read that i need to use the extern "C" construction but i don't seem to be doing it right.
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.