夏日炎炎,大家来寻找并和大家分享些小清凉小清新的东东吧:) 1.可以是旅行照片,图片,音乐,诗歌,文字,食物食谱,活动etc,anything you think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why you think it's cool; 2.欢迎鼓励原创,不拒绝转载转帖,请表明参加文海活动。每个id参加的每个topic奖励10伪币,每个id最高奖励上限50伪币; 3.活动即时开始,本周日美东时间午夜结束。 活动最终解释权归文海版务组所有。:)
i*p
5 楼
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. -------------------------------------
think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why you think it's cool; 奖励10伪币,每个id最高奖励上限50伪币;
【在 b*********k 的大作中提到】 : 夏日炎炎,大家来寻找并和大家分享些小清凉小清新的东东吧:) : 1.可以是旅行照片,图片,音乐,诗歌,文字,食物食谱,活动etc,anything you think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why you think it's cool; : 2.欢迎鼓励原创,不拒绝转载转帖,请表明参加文海活动。每个id参加的每个topic奖励10伪币,每个id最高奖励上限50伪币; : 3.活动即时开始,本周日美东时间午夜结束。 : 活动最终解释权归文海版务组所有。:)
t*t
10 楼
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
【在 a*********7 的大作中提到】 : 等看奔! : : think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why : you think it's cool; : 奖励10伪币,每个id最高奖励上限50伪币;
b*i
15 楼
那就用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,
1. If you have green card, the answer is YES. 2. If you do not have green card, the answer is NO.
c*e
22 楼
贴个证据来,咱们光明正大的吃
【在 j**o 的大作中提到】 : 发包子庆祝八卦版荣膺三甲。 : 我本人不介意多吃两个
s*n
23 楼
恐怖片给我感觉听chilly
think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why you think it's cool; 奖励10伪币,每个id最高奖励上限50伪币;
【在 b*********k 的大作中提到】 : 夏日炎炎,大家来寻找并和大家分享些小清凉小清新的东东吧:) : 1.可以是旅行照片,图片,音乐,诗歌,文字,食物食谱,活动etc,anything you think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why you think it's cool; : 2.欢迎鼓励原创,不拒绝转载转帖,请表明参加文海活动。每个id参加的每个topic奖励10伪币,每个id最高奖励上限50伪币; : 3.活动即时开始,本周日美东时间午夜结束。 : 活动最终解释权归文海版务组所有。:)
b*i
24 楼
我认为C语言mess up了。 static居然有两个意思,一个是static lifetime(not local variable),一个是 intern storage(not extern)。 这个书讲的应该是说lifetime。你这里又不能写static,写了就在其他文件不可见了。
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.
【在 s**********n 的大作中提到】 : 恐怖片给我感觉听chilly : : think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why : you think it's cool; : 奖励10伪币,每个id最高奖励上限50伪币;
b*i
32 楼
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.
这可能是他没注意, 但是从他的目的来说, 用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.
c*e
44 楼
我说呢。。他俩有点像。 不知道算不算夫妻相。
【在 f******r 的大作中提到】 : 我是kua, 笨死你算了
b*k
45 楼
有小新类型的小朋友,不信你问气龙。。。
【在 s**********n 的大作中提到】 : see, make sense : 清凉美女,不适合小朋友。。。
m*5
46 楼
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; }
think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why you think it's cool; 奖励10伪币,每个id最高奖励上限50伪币;
【在 b*********k 的大作中提到】 : 夏日炎炎,大家来寻找并和大家分享些小清凉小清新的东东吧:) : 1.可以是旅行照片,图片,音乐,诗歌,文字,食物食谱,活动etc,anything you think can give us a chilly feeling,如果有需要,请附若干说明文字分享一下why you think it's cool; : 2.欢迎鼓励原创,不拒绝转载转帖,请表明参加文海活动。每个id参加的每个topic奖励10伪币,每个id最高奖励上限50伪币; : 3.活动即时开始,本周日美东时间午夜结束。 : 活动最终解释权归文海版务组所有。:)
i*p
49 楼
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.