由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++: Static initialization dependency
相关主题
static initialization dependency c++[合集] which design pattern is used if a static variable insid
What're the three types of memory allocated for C++ variables?static 变量放在哪里?C++
c++ 不自动initialize变量么?C++ InitializeCriticalSection问题
static vector 怎么 initialize ? error LNK2001:的错误如何改正?
关于C/C++里的Static variable的memory allocation/initializa一道 memset in C++的题
Test your C++ knowledge...C++默认的copy constructor的疑惑
一个极简单的程序求教抠字眼:assignment and initialize in C++
will static/global var be initialized to 0 in C/C++C++ 全局变量是怎么回事?
相关话题的讨论汇总
话题: static话题: c++话题: oof话题: file
进入Programming版参与讨论
1 (共1页)
i**p
发帖数: 902
1
I have questions after reading "Thinking in C++" volume 1, Chapter 10 Name
Control, section Static initialization dependency. Hopefully people here can
explain them to me.
1. Why does it mention "static objects"? In fact there is no static object
in the example code but global class objects only.
2. Because of the dependency, the C++ compiler "should" guarantee the order,
right? Otherwise it is a bug.
Here is from the book.
---------------------------------------------------------
there is no guarantee concerning the order of initialization of static
objects across translation units, and the
language provides no way to specify this order. This can cause significant
problems. As an example of an instant disaster
(which will halt primitive operating systems and kill the process on
sophisticated ones), if one file contains
//: C10:Out.cpp {O}
// First file
#include
std::ofstream out("out.txt"); ///:~
and another file uses the out object in one of its initializers
//: C10:Oof.cpp
// Second file
//{L} Out
#include
extern std::ofstream out;
class Oof {
public:
Oof() { std::out << "ouch"; }
} oof;
int main() {} ///:~
the program may work, and it may not. If the programming environment builds
the program so that the first file is
initialized before the second file, then there will be no problem. However,
if the second file is initialized before the first,
the constructor for Oof relies upon the existence of out, which hasn't been
constructed yet and this causes chaos.
-------------------------------------
t****t
发帖数: 6806
2
the compiler doesn't guarantee order. it is the programmer's responsibility
to guarantee there is no dependency.
套句老乔的话, this is feature, not bug.

can
order,

【在 i**p 的大作中提到】
: I have questions after reading "Thinking in C++" volume 1, Chapter 10 Name
: Control, section Static initialization dependency. Hopefully people here can
: explain them to me.
: 1. Why does it mention "static objects"? In fact there is no static object
: in the example code but global class objects only.
: 2. Because of the dependency, the C++ compiler "should" guarantee the order,
: right? Otherwise it is a bug.
: Here is from the book.
: ---------------------------------------------------------
: there is no guarantee concerning the order of initialization of static

b***i
发帖数: 3043
3
那就用singleton呗

responsibility

【在 t****t 的大作中提到】
: the compiler doesn't guarantee order. it is the programmer's responsibility
: to guarantee there is no dependency.
: 套句老乔的话, this is feature, not bug.
:
: can
: order,

i**p
发帖数: 902
4
then what about question 1?

【在 b***i 的大作中提到】
: 那就用singleton呗
:
: responsibility

b***i
发帖数: 3043
5
我认为C语言mess up了。
static居然有两个意思,一个是static lifetime(not local variable),一个是
intern storage(not extern)。
这个书讲的应该是说lifetime。你这里又不能写static,写了就在其他文件不可见了。

【在 i**p 的大作中提到】
: then what about question 1?
i**p
发帖数: 902
6
I think this is author's mistake. In the context he says "the order of
initialization of static objects across translation units", but the code
example is non-static global object. In short, the example doesn't match the
context.

【在 b***i 的大作中提到】
: 我认为C语言mess up了。
: static居然有两个意思,一个是static lifetime(not local variable),一个是
: intern storage(not extern)。
: 这个书讲的应该是说lifetime。你这里又不能写static,写了就在其他文件不可见了。

b***i
发帖数: 3043
7
linkage在这里是无关的,这里讲的是lifetime,不是local variable

the

【在 i**p 的大作中提到】
: I think this is author's mistake. In the context he says "the order of
: initialization of static objects across translation units", but the code
: example is non-static global object. In short, the example doesn't match the
: context.

i**p
发帖数: 902
8
You use more terms. The 2 terms are "storage" and "linkage". Your term "
lifetime" is "storage", right?

【在 b***i 的大作中提到】
: linkage在这里是无关的,这里讲的是lifetime,不是local variable
:
: the

b***i
发帖数: 3043
9
准确的说,是相关的

【在 i**p 的大作中提到】
: You use more terms. The 2 terms are "storage" and "linkage". Your term "
: lifetime" is "storage", right?

t****t
发帖数: 6806
10
这可能是他没注意, 但是从他的目的来说, 用static (class or not) object 或者glo
bal object都没什么差别, 关键在于这些都是程序一开始需要初始化的对象. bihai说得
很清楚了, lifetime才是重要的.

the

【在 i**p 的大作中提到】
: I think this is author's mistake. In the context he says "the order of
: initialization of static objects across translation units", but the code
: example is non-static global object. In short, the example doesn't match the
: context.

m********5
发帖数: 17667
11
fix addr? visibility? linkage? Init Destro
static local Y local internal first call app end
global Y cross units external before main app end
static global Y compiling unit internal before main app end
local N local internal each call call end
You've seen those names are very twisted.
The global/static are actually the same thing with different visibility.
They all get stored in static memory instead of automatic memory or free
store. So in terms of the storage both `global` and `static` keywords label
`statically stored` variables.
The global variables will have the issue of initialization order fiasco.
Because they will get initialized before main(). However, the initialization
of static locals usually can be controlled because they are initialized at
the first time get called.
To avoid this fiasco, one need to use static local instead of global. Thus
the singleton or a initializing function is a good option.
int& x()
{
static int x0;
//do initialization
return x0;
}

【在 b***i 的大作中提到】
: 我认为C语言mess up了。
: static居然有两个意思,一个是static lifetime(not local variable),一个是
: intern storage(not extern)。
: 这个书讲的应该是说lifetime。你这里又不能写static,写了就在其他文件不可见了。

i**p
发帖数: 902
12
Thanks! We all agree it addresses the "global" meanings of the variable.
Because the "static" in C++ has both storage and linkage meanings, I prefer
to use "global" in stead of the "static" in this context. It is less
confusing.

label

【在 m********5 的大作中提到】
: fix addr? visibility? linkage? Init Destro
: static local Y local internal first call app end
: global Y cross units external before main app end
: static global Y compiling unit internal before main app end
: local N local internal each call call end
: You've seen those names are very twisted.
: The global/static are actually the same thing with different visibility.
: They all get stored in static memory instead of automatic memory or free
: store. So in terms of the storage both `global` and `static` keywords label
: `statically stored` variables.

1 (共1页)
进入Programming版参与讨论
相关主题
C++ 全局变量是怎么回事?关于C/C++里的Static variable的memory allocation/initializa
码工试题 (转载)Test your C++ knowledge...
C++里面如何最方便的表示这个数组的数组?一个极简单的程序求教
再问C++初始化问题。will static/global var be initialized to 0 in C/C++
static initialization dependency c++[合集] which design pattern is used if a static variable insid
What're the three types of memory allocated for C++ variables?static 变量放在哪里?C++
c++ 不自动initialize变量么?C++ InitializeCriticalSection问题
static vector 怎么 initialize ? error LNK2001:的错误如何改正?
相关话题的讨论汇总
话题: static话题: c++话题: oof话题: file