精華區beta C_and_CPP 關於我們 聯絡資訊
先從第1誡開刀: 01. 你不可以使用尚未給予適當初值的變數 錯誤例子: int accumulate(int max) /* 從 1 累加到 max,傳回結果 */ { int sum; /* 未給予初值的區域變數,其內容值是垃圾 */ int num; for (num = 1; num <= max; num++) { sum += num; } return sum; } 正確例子: int accumulate(int max) { int sum = 0; /* 正確的賦予適當的初值 */ int num; for (num = 1; num <= max; num++) { sum += num; } return sum; } ================================================================ 應該修改的地方: - 如果變數不為以下屬性(預設為auto)或動態配置(malloc), 其初始值為undefined behavior: * static * _Thread_local * global variable (這個我在Standard沒找到) Standard對應部份: 5.1.2 Execution environments 1. ... All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified. Program termination returns control to the execution environment. 靜態儲存(static storage duration,怎麼翻比較好阿?)的物件必須被初始化 並給予初始值,其他不用(其他的就是第1誡所表達的) 6.2.4 Storage durations of objects 3. An object whose identifier is declared without the storage-class specifier _Thread_local (C11新增?), and either with external or internal linkage(這啥?) or with the storage-class specifier static, has static storage duration. Its 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.... (這是我在Deep C的slide上看到的) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 90.41.60.207 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1460804200.A.EED.html
Frozenmouse: http://stackoverflow.com/questions/1358400 04/16 19:13
Frozenmouse: 這邊有人提到 internal linkage 04/16 19:13
Frozenmouse: copy 錯了,我要連的是第一個答案 Orz 04/16 19:14
Frozenmouse: 不過我覺得01講的重點是要養成手動初始化變數的習慣 04/16 19:16
Frozenmouse: 剩下的作為補充資料即可 04/16 19:17
我是覺得還是要加一兩行註明,我希望儘可能不要給新手錯誤的觀念。 當然C standard等等會作為補充資料(新手通常沒耐心看太長的文XD)
tinlans: shall be initizlied (...) before program startup 04/16 20:45
tinlans: initialized 04/16 20:46
tinlans: 不要因為中間有個括號,before program startup 就漏了XD 04/16 20:46
wtchen: 所以不一定是0? 04/16 21:17
※ 編輯: wtchen (90.41.60.207), 04/16/2016 21:18:52 > -------------------------------------------------------------------------- < 作者: tinlans ( ) 看板: C_and_CPP 標題: Re: [討論] 請板友幫忙review置底13誡--N.01 時間: Sat Apr 16 21:39:06 2016 ※ 引述《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上看到的)