看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) http://tinyurl.com/y4hq4vfg 問題(Question): submit static int 無法初始化? 餵入的資料(Input): static volatile int i = 0 ; 預期的正確結果(Expected Output): Stdout: init i i1 = 0 i2 = 0 i1 = 1 i2 = 1 i1 = 2 錯誤結果(Wrong Output):i Stdout: runtime error: reference binding to null pointer of type 'struct value_type' (stl_vector.h) - leet code spiral 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) void reverseString(vector<char>& s) { static int i = 0 ; static char *p = nullptr; if( &s[0] != p ) { p = &s[0]; i = 0; cout << "init i " << endl; } cout << "i1 = " << i << endl; if( i >= (s.size()/2) ) return; cout << "i2 = " << i << endl; swap( s[i] , s[s.size()-1-i]); i++; reverseString(s); } 補充說明(Supplement): 我的i怎不會跑... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.10.126.116 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1552301160.A.F50.html ※ 編輯: IhateOGC (39.10.126.116), 03/11/2019 20:38:57 已解決 void reverseString(vector<char>& s) { static int i = 0 ; static char *p = nullptr; if( s.size() != 0 && (&s[0] != p) ){ p = &s[0]; i = 0; } if( i >= (s.size()/2) ) return; swap( s[i] , s[s.size()-1-i]); i++; reverseString(s); } ※ 編輯: IhateOGC (39.10.126.116), 03/11/2019 20:51:19
CoNsTaR: 因為 i 不會自己重置回 0 ? 03/11 20:57
IhateOGC: 不會...xd 03/11 21:51
kokal: 看output是 s是NULL時,p=&s[0]發生錯誤 03/11 22:47