Re: [讨论] 请板友帮忙review置底13诫--N.01

楼主: tinlans ( )   2016-04-16 21:39:06
※ 引述《wtchen (没有存在感的人)》之铭言:
: 应该修改的地方:
: - 如果变量不为以下属性(默认为auto)或动态配置(malloc),
: 其初始值为undefined behavior:
indeterminate
: * static
: * _Thread_local
: * global variable (这个我在Standard没找到)
6.7.9 Initialization
If an object that has automatic storage duration is not initialized explicitly,
its value is indeterminate. If an object that has static or thread storage
duration is not initialized explicitly, then:
...
- if it has arithmetic type, it is initialized to (positive or
unsigned) zero;
...
global variable 所拥有的是 static storage duration,所以初值会是正零或无号零。
但是因为国内很多人用 MCU,要小心有的编译器会允许你甚至默认就违反这条规则。
: _Thread_local (C11新增?), and either with external or internal linkage
: (这啥?) or with the storage-class specifier static, has static storage
: duration. Its
上面 6.2.2 Linkages of identifiers 就在讲 linkage 啊。
然后 paragraph 5:
If the declaration of an identifier for a function has no storage-class
specifier, its linkage is determined exactly as if it were declared with
the storage-class specifier extern. If the declaration of an identifier
for an object has file scope and no storage-class specifier, its linkage
is external.
什么叫 an object has file scope and no storage-class specifier?
就是前面没有标 extern 或 static 的 global variable,默认是 external linkage。
但是不管它是 static 还是 external linkage,它都拥有 static storage duration。
为什么?因为上面你贴的那段就是这样讲了啊。
虽然规格书讲得比较隐晦,但是其实用消去法也能发现该找哪边才对。
翻一下规格书对 linkage 及 storage duration 的分类就会看到这两句:
1. There are three kinds of linkage: external, internal, and none.
2. There are four storage durations: static, thread, automatic, and allocated.
global variable 这个英文词组在规格书里没被直接归类,但必然还是在这游戏规则里。
你先把不可能的消去,然后去试图证明它是你想像的那类,就能找出对应的文字叙述。
: lifetime is the entire execution of the program and its stored value is
: initialized only once, prior to program startup.
: static storage duration 包含:
: - _Thread_local (C11新增?)
: - 含有static 修饰字的变量
: 不过这边似乎没提到global variable也是被初始化为0....
因为 global variable 这个英文词组在标准规格书里几乎不使用。
: (这是我在Deep C的slide上看到的)
作者: wtchen (没有存在感的人)   2016-04-16 21:41:00
感谢补充,最近才开始真正看standard,真的有好多眉角...所以indeterminate != undefined behavior?感谢说明,我想借由这次13诫review的机会好好看一下standard,不管是新手还是老手都能受用。感谢,受教了,我还要再努力阿
作者: legendmtg (CLANNAD)   2016-04-16 23:26:00
大神出现了 <(_ _)>
作者: VictorTom (鬼翼&娃娃鱼)   2016-04-18 01:25:00
有神快拜...<(_ _)>
作者: mabinogi805 (焚离)   2016-04-18 03:44:00
谢谢,受教了!<(_ _)>

Links booklink

Contact Us: admin [ a t ] ucptt.com