Article View: comp.std.c++
Article #3789initialization of nonlocal static objects
From: Jeffrey C. Gealow
Date: Sun, 24 Mar 1996 04:40
Date: Sun, 24 Mar 1996 04:40
119 lines
4164 bytes
4164 bytes
I've encountered trouble with the initialization of nonlocal static
objects. The following parts of the ARM are relevant:
ARM 3.4
The initialization of nonlocal static objects in a translation unit is
done before the first use of any function or object defined in that
translation unit. Such initializations may be done before the first
statement of main() or deferred to any point in time before the first
use of a function or object defined in that translation unit. The
default initialization of all static objects to zero is performed
before any dynamic (that is, run-time) initialization. No further
order is imposed on the initialization of objects from different
translation units.
ARM 3.4 annotation
A useful technique for insuring that a global object is initialized
only once and before its first use is to maintain a count of the
number of translation units using it.
ARM 7.1.6
A const object may be initialized, but its value may not be changed
thereafter.
Are the following observations correct?
The technique described in the ARM 3.4 annotation cannot be applied to
const objects since the values of the objects may not be changed.
The ARM 3.4 guarantee that the "initialization of nonlocal static
objects in a translation unit is done before the first use of any
function or object defined in that translation unit" is not accurate.
Functions or objects defined in a translation unit may be used in the
initialization of nonlocal static objects defined in other translation
units. Therefore, mutual dependencies may make it impossible to honor
the guarantee.
The C++ standard could be revised to include a guarantee that whenever
possible, the initialization of nonlocal static objects in a translation
unit is done before any function or object in that translation unit is
used to initialize objects in other translation units. Otherwise,
const objects should not be used.
The Sun SPARCompiler C++ 4.1 and G++ 2.7.2 don't even honor the ARM
guarantee when there are no mutual dependencies. The following
example illustrates the problem. There are four files: library.cc,
library.h, luser.cc, and main.cc. The behavior of the program depends
on the order in which files are specified in the compile command:
gator jgealow % $CC main.cc luser.cc library.cc
gator jgealow % ./a.out
global: 0
local: 6
gator jgealow % $CC main.cc library.cc luser.cc
gator jgealow % ./a.out
global: 6
local: 6
Jeff
::::::::::::::
library.h
::::::::::::::
class X {
public:
int i;
X(int);
};
class Y {
public:
int i;
Y();
};
::::::::::::::
library.cc
::::::::::::::
#include "library.h"
const X Int_(0);
const X Int0(1);
const X Int1(2);
const X Intx(3);
X :: X( int x) : i(x)
{}
Y :: Y( )
{
i = Int_.i + Int0.i + Int1.i + Intx.i;
}
::::::::::::::
luser.cc
::::::::::::::
#include "library.h"
Y global;
::::::::::::::
main.cc
::::::::::::::
#include <iostream.h>
#include "library.h"
extern Y global;
int main(int, char* argv[])
{
Y local;
cout << "global: " << global.i << endl;
cout << "local: " << local.i << endl;
}
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<4j1tm6$bmu@senator-bedfellow.MIT.EDU>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backShow Full Headers
Complete Article Headers
From: jgealow@mtl.mit.edu (Jeffrey C. Gealow) Message-ID: <4j1tm6$bmu@senator-bedfellow.MIT.EDU> X-Original-Date: 23 Mar 1996 22:24:06 GMT Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 24 Mar 96 04:40:44 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: initialization of nonlocal static objects Organization: MIT Microsystems Technology Laboratories X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMVTSXeEDnX0m9pzZAQEakgF9Gk3zeheK3b8vug16Av9z+hak3HJaev22 hWIPYUXLd5fZMxE5uk+5AIv0m1NJH5v4 =edOA