EE_DSnP 板


LINE

有同學寫了這樣的程式: int x; cin >> x; int a[x]; // ... 使用 a[x]... 結果沒有問題且執行正確. 理論上 int a[x] 的 size 要是 constant 然後 compiler 才能在 compile time 決定 sizeof(a). (還記得 sizeof 是 compile time operator). 那這裡是怎麼回事呢? 我做了一個實驗: ============================================== int main() { int x; cin >> x; int a[x]; cout << "sizeof(a) = " << sizeof(a) << endl; cout << "&a = " << &a << endl; cout << " a = " << a << endl; cout << "&a[0] = " << &a[0] << endl; cout << "&a[1] = " << &a[1] << endl; cout << "&a[9] = " << &a[9] << endl; int b[10]; cout << "sizeof(b) = " << sizeof(b) << endl; cout << "&b = " << &b << endl; cout << " b = " << b << endl; cout << "&b[0] = " << &b[0] << endl; cout << "&b[1] = " << &b[1] << endl; cout << "&b[9] = " << &b[9] << endl; int c[10]; cout << "&c = " << &c << endl; cout << " c = " << c << endl; cout << "&c[0] = " << &c[0] << endl; cout << "&c[1] = " << &c[1] << endl; cout << "&c[9] = " << &c[9] << endl; } ============================================= 結果輸出是 (on Linux): 10 sizeof(a) = 40 &a = 0xfef7e770 a = 0xfef7e770 &a[0] = 0xfef7e770 &a[1] = 0xfef7e774 &a[9] = 0xfef7e794 sizeof(b) = 40 &b = 0xfef7e7f0 b = 0xfef7e7f0 &b[0] = 0xfef7e7f0 &b[1] = 0xfef7e7f4 &b[9] = 0xfef7e814 &c = 0xfef7e7c0 c = 0xfef7e7c0 &c[0] = 0xfef7e7c0 &c[1] = 0xfef7e7c4 &c[9] = 0xfef7e7e4 We can see: 1. sizeof(a) = 40!! 所以顯然 sizeof(a) 並不是 compile time 就決定的, 因為 x 是 cin 才指定為 10 的. 查了許多網頁都是一面倒的說 sizeof is a compile time operator... 只有一個 IBM Mac OS X/XL C++ compiler 說: "At compile time, the compiler analyzes the expression to determine its type, but does not evaluate it...." Anyway, it's a mystery... 2. a 的 memory address 比 b and c 小!! Since stack memory gives away its memory in the reverse addressing order, that means 程式先給了 b[10] and c[10] 的 memory space 才給 a[10]. 也就是說, 程式進入 main() 之後就先將 local varible b[10], c[10] 的 memory 準備好, 然後執行到 a[x] 才去決定要用多少 memory 來存 a. =================================== 喔, 對了, 對 int a[x] 如果 x is not a constant 是不能用 initializer list 來 initialize a[x] 的值 (e.g. int a[x] = { 0 } ). 因為 sizeof({0}) 還是有可能比 sizeof(a[x]) 還要大... --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.121.132.121 ※ 編輯: ric2k1 來自: 59.121.132.121 (12/05 22:28)
1F:推 ilway25:選課程式有用到這樣,G++過,VC不給過.. 12/05 22:44
2F:推 ric2k1:不過忘了說, 這樣寫是不好的... 12/05 23:03
3F:推 ilway25:所以還是用pointer new比較好? 話說strtok後要不要delete? 12/05 23:06
4F:推 timrau:This is a GCC extension. 12/05 23:28
5F:推 timrau:http://tinyurl.com/yt9gr3 12/05 23:30
6F:推 ric2k1:Cool, so it is safe if the compiler supports it. 12/05 23:41
7F:推 ric2k1:strtok 不用 "額外" 去 delete. 12/05 23:42
8F:推 lionel20002:原來如此 話說我常這樣寫... 12/06 00:37







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP