C++設計實現演算法時易犯錯誤

來源:互聯網
上載者:User

標籤:href   htm   test   c++   標點符號   log   erro   div   實現   

 

1.未初始化

指標未初始化為NULL,造成判斷條件出錯

數值未初始化為0,造成數組超界,記憶體流失

 2.

long long輸入時

lld or I64d

寫成d一定出錯

 

3.gcc編譯報錯:程式中有游離的‘\357’‘\273’‘\277’等

感謝http://www.cnblogs.com/lidp/archive/2009/06/17/1697886.html

 

造成的原因主要有兩個:
1. 程式(*.c,*.h)中使用了中文的標點符號(全形),比如;,},+。
改成英文的標點半形符號就行了。
甚至有時候空格也會出現類似錯誤,刪掉該空格 重新輸入。
vim裡面做類似替換還是很容易的。

如何看到報錯的符號?
od -c hello.c > log.txt
在log中就能看到符號了

2. 
如果替換成了英文標點還出錯的話,還報此錯誤,那麼就是檔案存貯格式的問題了。
一般在windows下的檔案都存成ansci格式,為了在linux下能通用,建議儲存成UTF-8不帶BOM 
編碼格式,因為目前gcc和g++不支援UTF-8帶BOM編碼格式。

用g++編譯的時候碰到UTF-8 BOM錯誤怎麼辦?
$ g++ -I../../include unit_test.cpp -o unit_test
unit_test.cpp:1: 錯誤: 程式中有游離的‘\357‘
unit_test.cpp:1: 錯誤: 程式中有游離的‘\273‘
unit_test.cpp:1: 錯誤: 程式中有游離的‘\277‘
In file included from unit_test.cpp:63:
...

或在英文系統下:
$ g++ -I../../include unit_test.cpp -o unit_test
unit_test.cpp:1: error: stray ‘\357‘ in program
unit_test.cpp:1: error: stray ‘\273‘ in program
unit_test.cpp:1: error: stray ‘\277‘ in program
In file included from unit_test.cpp:63:
...


如何判斷檔案是否是使用UTF-8 BOM儲存的?

執行下面的命令:
$ cat cpp/src/unit_test/unit_test.cpp |hd -n 10
00000000 ef bb bf 2f 2a 2a 2a 2a 2a 2a |.../******|
0000000a

 

C++設計實現演算法時易犯錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.