[問題] static 變數在function 中 與 memory
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
void foo(){
static int x;
}
int main ()
{
foo();
return 0;
}
問題是這樣的。 在foo function 中,static 變數x ,會被放在
memory 的 data section 中(或是有些人說的golable 區域中)。
問題: foo() return 後,從stack 裡移除之後。
1. 就我知道,static 變數x ,仍然還在data section 裡面。
x 位址仍然有效。
2. 如果上面的想法是對的。 那這樣如果一個大程式,
我看很多fucntion 都用static 變數在做事。這樣不是data section 越長越大,
沒人清那些垃圾?這樣是對的嗎?
請教了。
謝謝
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.248.212.6
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1401546312.A.37E.html
※ 編輯: YOUHAVE (111.248.212.6), 05/31/2014 22:26:17
→
05/31 22:54, , 1F
05/31 22:54, 1F
→
05/31 22:54, , 2F
05/31 22:54, 2F
→
05/31 22:59, , 3F
05/31 22:59, 3F
→
05/31 23:00, , 4F
05/31 23:00, 4F
→
05/31 23:00, , 5F
05/31 23:00, 5F
→
05/31 23:08, , 6F
05/31 23:08, 6F
→
05/31 23:35, , 7F
05/31 23:35, 7F
推
06/01 07:01, , 8F
06/01 07:01, 8F
→
06/01 12:42, , 9F
06/01 12:42, 9F
→
06/01 16:04, , 10F
06/01 16:04, 10F
→
06/08 08:41, , 11F
06/08 08:41, 11F
→
06/08 08:43, , 12F
06/08 08:43, 12F