--- tags: algorithms --- # [Leetcode] #1 Two Sum https://leetcode.com/problems/two-sum/ -- ## 遇到編譯問題 [source code](https://github.com/allenCheng1984/leetcode-notes/tree/master/1.%20Two%20Sum/c) 正常運作 ![](https://hackmd.io/_uploads/Syzxv9CFo.png) 要嘗試 refactor code, 在執行出錯 ![](https://hackmd.io/_uploads/BJVQvcRFi.png) 發現問題出在這行 ![](https://hackmd.io/_uploads/Sy0mP9AFo.png) 為什麼單純宣告 int array 會出現 `segmentation fault`? 宣告 char array 卻沒影響? ![](https://hackmd.io/_uploads/H15nw5AKs.png) --- ## ptt 版友的協助 ([link](https://www.ptt.cc/bbs/C_and_CPP/M.1672551970.A.87A.html)) 開啟 `-wAll` 把所有的 warning 秀出來,顯示第十行要初始化 ![](https://hackmd.io/_uploads/B145nnCFi.png) -- but 補上去後,編譯過了,但執行反而出問題 ... Orz ![](https://hackmd.io/_uploads/S1hjhnCKj.png) ![](https://hackmd.io/_uploads/ByJ32nAKs.png) -- ## 問題已解決 原來是只要傳入 function 裡的 argument pointer 都要用 malloc ... Orz [git commit](https://github.com/allenCheng1984/leetcode-notes/commit/dd339f787bd16ab956cb83fff8410314e5491ae4) ![](https://hackmd.io/_uploads/BJFkg6Cti.png) -- ## Final: 補上 Makefile 的編譯器參數、修正報出來的警告與 coding style fix https://github.com/allenCheng1984/leetcode-notes/commit/b7e6d37afe83f5ed09d928188d44dfb0c98451de