




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、C+編程的調(diào)試技巧,主要內(nèi)容,VC+集成開發(fā)環(huán)境 調(diào)試方法 如何獨(dú)立解決問題 編程規(guī)范,一、集成開發(fā)環(huán)境的熟悉,保存、全部保存,打開最近的工程,給工程中添加文件,查找文件,在指定目錄下的某類型文件中搜索所有包含某字符串的文件,Debug:帶有調(diào)試信息,文件大。 Release:沒有調(diào)試信息,不能調(diào)試,文件小,切換Debug模式和Release模式,比較文件大小,編譯(ctrl+F7):編譯源文件到目標(biāo)文件 構(gòu)建(F7):鏈接目標(biāo)文件和庫函數(shù)為可執(zhí)行文件,如無目標(biāo)文件則先生成 清潔:刪掉debug或者release版本的所有中間文件和可執(zhí)行文件 調(diào)試 單步執(zhí)行(F10):單步執(zhí)行,遇到函數(shù)調(diào)用時(shí)
2、把其當(dāng)作一條語句執(zhí)行 深入函數(shù)的單步執(zhí)行(F11):單步執(zhí)行,遇到函數(shù)調(diào)用是深入到其內(nèi)部 執(zhí)行到光標(biāo)處(ctrl+F10):一次執(zhí)行完光標(biāo)前的所有語句,并停到光標(biāo)處 跳出(shift+F11):執(zhí)行完當(dāng)前函數(shù)的所有剩余代碼,并從函數(shù)跳出 重新開始調(diào)試(ctrl+shift+F5):重新開始調(diào)試過程 結(jié)束調(diào)試(shift+F5):執(zhí)行完程序的剩余部分,結(jié)束調(diào)試 設(shè)置/取消斷點(diǎn)(F9):在某一行設(shè)置和取消斷點(diǎn),當(dāng)前執(zhí)行的語句,堆棧的內(nèi)容:函數(shù)調(diào)用關(guān)系,從這里可以 查看內(nèi)存數(shù)據(jù),程序執(zhí)行過程中的一 些變量會(huì)顯示在這里,著名的watch窗口:看變量 表達(dá)式、地址等各種信息,寄存器當(dāng)前值,二、調(diào)試方法,
3、樹立正確的編程方法論,沒有解決不了的問題,樹立編程信心 按照正確的方法來編程 從實(shí)踐中體會(huì)解決問題的思路、編程的思想 熟練掌握語法,數(shù)據(jù)結(jié)構(gòu),算法、模式,底層環(huán)境 按照編程規(guī)范來編寫代碼 充分利用各種資源: 調(diào)試器 msdn 網(wǎng)絡(luò)搜索引擎,常用調(diào)試手段,利用編譯器的輸出信息排除錯(cuò)誤 利用調(diào)試器的調(diào)試功能 單步、run to cursor、斷點(diǎn)、條件斷點(diǎn)、查看堆棧、 利用watch窗口查看變量、表達(dá)式的值 分段調(diào)試法、增量調(diào)試法 利用flush人為刷新緩沖區(qū) 修改輸出信息,增加標(biāo)志位 利用輸出語句打印調(diào)試信息 通過堆棧觀察函數(shù)調(diào)用情況 綜合程序調(diào)試方法 注釋的技巧、條件編譯,幾個(gè)簡單例子,#in
4、clude #include void main() cout=1;j/=2)if(d/j=1)cout1;if(d/j=0) cout0;d=d%j;coutsetw(10)octisetw(10)hexiendl; ,一個(gè)編程風(fēng)格非常糟糕的例子:,/*FILE COMMENT* * System Name : for eduction (NO TRANSFERRING) * File Name : e05b.c * Contents : embedded C language entrance course * : exercise 5B: program using pointer *
5、: 乮pass one-dimensional array to function) * Model : for OAKS8-LCD Board * CPU : R8C/Tiny series * Compiler : NC30WA(V.5.30 Release 1) * OS : not be used * Programmer : RENESAS Semiconductor Training Center * Note : for OAKS8-R5F21114FP(R8C/11 group,20MHz) * * COPYRIGHT(C) 2004 RENESAS TECHNOLOGY CO
6、RPORATION * AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED * * History : *FILE COMMENT END*/ /* include file */ #include defs.h/* define common symbol */ #include oaks8lib.h/* for function to deal with OAKS8-LCDBoard peripheral*/ /* define function prototype */ int main(void); static void get
7、_string(unsigned char *message, unsigned char *buff); /* input string*/ static int get_strlength(unsigned char *str);/* get string length */ static void print_dec(unsigned char *message, int dec); /* display decimal number in LCD*/ /*FUNC COMMENT* * ID : 1.0 * function name : main * function : call
8、function to get string length, display result * parameter : none * return : result 0:normal end * function used : get_strlength * notice : none * History : *FUNC COMMENT END*/ int main(void) unsigned char buff10+1; /* define variable to save string input from key matrix*/ int count;/* define variabl
9、e for save number of character */ while(1) /* input string from key matrix */ get_string(Str=, buff); /* call function to get string length */ /* pass address of array saving string, get string length */ count = get_strlength(buff); /* display string length in LCD */ print_dec(Len=, count); return 0
10、; /*FUNC COMMENT* * ID : 1.1 * function name : get_string * function : input string from key matrix * parameter : message:pointer to input waiting message * : buff :pointer to space saving string input * return : none * function used : none * notice : none * History : *FUNC COMMENT END*/ static void
11、 get_string(unsigned char *message, unsigned char *buff) /* define function to input string*/ LCD_puts(message);/* display message*/ SW_gets(buff); /* input string from key matrix*/ LCD_puts(buff);/* display string input */ LCD_putchar(n);/* return */ /*FUNC COMMENT* * ID : 1.2 * function name : get
12、_strlength * function : refer string using pointer passed as argument, return length * parameter : str:address of string to count length * return : length * function used : none * notice : none * History : *FUNC COMMENT END*/ static int get_strlength(unsigned char *str) /* define function to get str
13、ing length*/ int len = 0;/* define and initialize variable for string length*/ /* refer every character in one-dimensional array buff pointed by */ /* pointer str, count up until detect (0) at end of string */ while(0 != *str+) len+;/* count number of character */ return len; /* return length*/ /*FU
14、NC COMMENT* * ID : 1.3 * function name : print_dec * function : display decimal number in LCD * parameter : message:pointer to input waiting message * : dec :decimal number to be displayed * return : none * function used : none * notice : none * History : *FUNC COMMENT END*/ static void print_dec(un
15、signed char *message, int dec) /* define function to display decimal number in LCD*/ LCD_puts(message);/* display message*/ LCD_putdec(dec);/*display decimal number in LCD*/ LCD_putchar(n);/* return */ /* end of file */,一個(gè)編程風(fēng)格良好的例子:,結(jié) 論 1,永遠(yuǎn)不要寫過長的語句,應(yīng)該讓代碼盡可能簡單;永遠(yuǎn)不要把兩條語句寫在一行中,以便于調(diào)試。,#include void mai
16、n() float num1,num2,outcome; char op,a; do coutnum1num2endl; coutopendl; if(num2!=0) switch(op) case *:outcome=num1*num2;break; case /:outcome=num1/num2;break; case +:outcome=num1+num2;break; case -:outcome=num1-num2;break; default:coutaendl; while(a=y); ,兩個(gè)數(shù)的算術(shù)運(yùn)算,/如果輸入字符a,則顯示1,否則程序結(jié)束 #include int f
17、() char c; coutc; if(c=a) return 1; else ; int main() coutf()endl; return 0; ,根據(jù)輸入給出輸出,結(jié) 論 2,要盡可能的熟悉語法知識(shí) 充分利用編譯器提供的信息 編程過程中要細(xì)心 思路要清晰,沒把握時(shí),要先畫流程圖,然后再編碼,#include int main() double a, b, c, d; coutabc; if(a=b ,判斷三角形的形狀,結(jié) 論 3,要掌握編程的本質(zhì): 是由程序員控制機(jī)器,使其按照我們的思路運(yùn)轉(zhuǎn),機(jī)器本身沒有智能。,#include void main() double integer1
18、,integer2,e,f,g,h; char op,a; cinop; coutinteger1; coutinteger2; switch(op). e=integer1+integer2; f=integer1-integer2; g=integer1*integer2; h=integer1/integer2; case +:coute; break; case -:coutf; break; case *:coutg; break; case /:couth; break; coutDo you want to continue(Y/N or y/n)?; ,兩個(gè)數(shù)的算術(shù)運(yùn)算,#in
19、clude void main() double integer1,integer2,e,f,g,h; char op,a; cinop; coutinteger1; coutinteger2; switch(op)./點(diǎn) e=integer1+integer2;/這段代碼執(zhí)行不到 f=integer1-integer2; g=integer1*integer2; h=integer1/integer2; case +:coute; break; case -:coutf; break; case *:coutg; break; case /:couth; break; cout“Do you
20、 want to continue(Y/N or y/n)?”; /標(biāo)記控制重復(fù) ,兩個(gè)數(shù)的算術(shù)運(yùn)算,結(jié) 論 4,編譯器不一定能給出確切的錯(cuò)誤信息,要合理的“猜測” 出錯(cuò)信息 通過單步運(yùn)行的方式加深我們對程序執(zhí)行流程的理解。 單步執(zhí)行是最重要的調(diào)試手段!,#include int main() char type = s; while(type != !) couttype; if (type = 65 ,判斷一個(gè)字符是什么類別,另一個(gè)通過單步執(zhí)行調(diào)試的例子,#include int main() char type = s; while(type != !) couttype; if (ty
21、pe = 65 ,判斷一個(gè)字符是什么類別,通過單步執(zhí)行可以搞清程序的執(zhí)行線路,以發(fā)現(xiàn)錯(cuò)誤點(diǎn),#include int main() int m; coutm; for(int i=1;i=m-1;i+) m=m*i; cout階乘為:mendl; return 0; ,求階乘,#include int main() int m; coutm; for(int i=1;i=m-1;i+) m=m*i;/循環(huán)體內(nèi)修改了循環(huán)條件 cout階乘為:mendl; return 0; ,求階乘,通過單步執(zhí)行可以監(jiān)視變量的變化,當(dāng)變量與預(yù)期不一致是,即找到了錯(cuò)誤點(diǎn),#include int main() i
22、nt m; coutm; int n=m; for(int i=1;i=n-1;i+) m=m*i; cout階乘為:mendl; return 0; ,求階乘,結(jié) 論 5,通過單步執(zhí)行可以發(fā)現(xiàn)程序運(yùn)行的軌跡 通過單步執(zhí)行可以隨時(shí)監(jiān)視變量的值,無論何時(shí)發(fā)現(xiàn)變量值與我們的預(yù)期不一致時(shí),即找到了錯(cuò)誤點(diǎn),單步調(diào)試方法,前提:debug版本可執(zhí)行程序,build正確 進(jìn)入調(diào)試狀態(tài) F10為單步調(diào)試,要深入到子函數(shù)中單步調(diào)試,應(yīng)在函數(shù)調(diào)用語句處按F11 單步調(diào)試應(yīng)該結(jié)合watch窗口監(jiān)視變量值的變化,稍復(fù)雜的例子,#include void main() for(int i=1;i=11-s;n-) c
23、out*;coutendl; coutendl; for(int z=1;z=10;z+) for(int p=9;p=10-z;p-) cout ; for(int q=1;q=z;q+) cout*;coutendl; coutendl; ,輸出星號,#include void main() for(int i=1;i=11-s;n-) cout*;coutendl; coutendl; for(int z=1;z=10;z+) for(int p=9;p=10-z;p-) cout ; for(int q=1;q=z;q+) cout*;coutendl; coutendl; ,輸出星號
24、,分析:程序比較長,輸出怪異,一時(shí)難以發(fā)現(xiàn)出錯(cuò)地點(diǎn),所以采用化整為零、化繁為簡的分段調(diào)試法,#include void main() for(int i=1;i=11-s;n-) cout*;coutendl; coutendl; for(int z=1;z=10;z+) for(int p=9;p=10-z;p-) cout ; for(int q=1;q=z;q+) cout*;coutendl; coutendl; ,輸出星號,使用到的調(diào)試方法: 分段調(diào)試 單步F10調(diào)試 run to cursor 強(qiáng)制輸出(flush) 用watch窗口觀察表達(dá)式的值,#include void ma
25、in() for(int i=1;i=z;p-) cout flush; for(int q=1;q=z;q+) cout*flush; coutendl; coutendl; ,調(diào)試后的程序,改正了循環(huán)中的邏輯問題,另外,本程序存在變量定義過多的缺點(diǎn),#include void main() for(int i=1;i=11-s;n-) cout*;coutendl; coutendl; for(int z=1;z=10;z+) for(int p=9;p=10-z;p-) cout ; for(int q=1;q=z;q+) cout*;coutendl; coutendl; ,輸出星號,
26、對錯(cuò)誤程序的進(jìn)一步分析: 為什么會(huì)出現(xiàn)黑屏?錯(cuò)誤 原因在哪里?可通過修改 源代碼,設(shè)置標(biāo)志位來驗(yàn)證,結(jié) 論 6,分段調(diào)試:可以有效降低調(diào)試難度,快速定位錯(cuò)誤的大致范圍,“粗調(diào)” 單步F10調(diào)試:找到可疑點(diǎn)后,逐行執(zhí)行可疑點(diǎn)附近的代碼,對錯(cuò)誤“精調(diào)” run to cursor:使程序快速運(yùn)行到可疑點(diǎn)后暫停,克服F10運(yùn)行速度慢的缺點(diǎn) 強(qiáng)制輸出(flush):增加有參考價(jià)值的信息 watch窗口:可觀察變量或表達(dá)式的值,配合F10進(jìn)行單步“精調(diào)”,小技巧,在循環(huán)體內(nèi),每執(zhí)行一次run to cursor,就相當(dāng)于完整的執(zhí)行了一遍循環(huán)體,經(jīng)常用來替代多次執(zhí)行F10。,/例 求階乘 #include
27、 int Factorial ( int ) ; void main () int k ; cout k ; cout k ! = Factorial(k) endl ; int Factorial ( int n ) if ( n = 0 ) return 1 ; else return n * Factorial ( n - 1 ) ; ,分析程序執(zhí)行流程,求階乘,/例 求階乘 #include int Factorial ( int ) ; void main () int k ; cout k ; cout k ! = Factorial(k) endl ; int Factorial
28、 ( int n ) if ( n = 0 ) return 1 ; else int m = n * Factorial ( n - 1 ); return m; ,簡化return語句,求階乘,/例 求階乘 #include int Factorial ( int ) ; void main () int k ; cout k ; cout k ! = Factorial(k) endl ; int Factorial ( int n ) if ( n = 0 ) return 1 ; else coutthe parameter of this call is: nendl; int m
29、 = n * Factorial ( n - 1 ); coutlast call go back here: nendl; return m; ,增加調(diào)試輸出信息,求階乘,結(jié) 論 7,F11可以深入到函數(shù)內(nèi)部單步執(zhí)行,F(xiàn)11和F10結(jié)合起來使用可以調(diào)試多函數(shù)的程序。 step over可以從當(dāng)前函數(shù)中跳出到調(diào)用點(diǎn) 觀察堆棧能看到函數(shù)之間的調(diào)用關(guān)系 在適當(dāng)?shù)奈恢貌迦胼敵稣Z句,有助于了解程序的執(zhí)行流程,是一種十分重要的調(diào)試手段,小技巧,單步調(diào)試時(shí),如果某條語句不是調(diào)用自定義的函數(shù),那么不要按F11,尤其是本行包含了cout,/例 求菲波那契數(shù)列的第五項(xiàng) #include int Fibonacc
30、i ( int n ) if ( n = 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; void main () cout Fibonacci(5) endl ; ,求菲波那契數(shù)列第五項(xiàng),請大家注意觀察堆棧的變化,綜合實(shí)例,#include #include #include int main() int m,i,k,n=0; bool prime=1; for(m=101;m=199;m+=2) k=int(sqrt(m); for(i=2;i=k;i+) if(m%i=0) prime=0; break
31、; if(prime) coutsetw(5)m; n+; if(n%10=0) coutendl; coutendlthe total number is: nendl; return 0; ,求100-200間的素?cái)?shù),#include #include #include int main() int m,i,k,n=0; bool prime=1; for(m=101;m=199;m+=2) k=int(sqrt(m); for(i=2;i=k;i+) if(m%i=0) prime=0; break; if(prime) coutsetw(5)m; n+; if(n%10=0) cout
32、endl; coutendlthe total number is: nendl; return 0; ,求100-200間的素?cái)?shù),用斷點(diǎn)來代替run to cursor,在光標(biāo)處設(shè)置斷點(diǎn)F9,運(yùn)行程序至斷點(diǎn)處F5,光標(biāo)放到第23行,設(shè)置條件斷點(diǎn),第23行的有效斷點(diǎn),第25行的無效斷點(diǎn),越過了前兩次循環(huán),停到了第三次,#include #include #include int main() int m,i,k,n=0; bool prime ; for(m=101;m=199;m+=2) prime=1; k=int(sqrt(m); for(i=2;i=k;i+) if(m%i=0) prime=0; break; if(prime) coutsetw
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 新聞出版行業(yè)數(shù)字化轉(zhuǎn)型規(guī)劃
- 游戲產(chǎn)業(yè)發(fā)展現(xiàn)狀及游戲公司的市場推廣戰(zhàn)略研究報(bào)告
- 甲方項(xiàng)目管理培訓(xùn)課件
- 生育保險(xiǎn)培訓(xùn)課件
- 《不斷發(fā)展的現(xiàn)代社會(huì)》教學(xué)設(shè)計(jì)
- 老年病藥物行業(yè)跨境出海項(xiàng)目商業(yè)計(jì)劃書
- 傳動(dòng)部件追蹤創(chuàng)新創(chuàng)業(yè)項(xiàng)目商業(yè)計(jì)劃書
- 江西省主要鄉(xiāng)土樹種名錄
- 2025學(xué)校關(guān)工委校園文化計(jì)劃
- 煤炭質(zhì)量管理暫行辦法
- 血管超聲檢查臨床應(yīng)用
- 2025年長沙市中考數(shù)學(xué)試卷真題(含標(biāo)準(zhǔn)答案)
- 2025年北京市中考數(shù)學(xué)試卷真題
- 教育政策執(zhí)行情況調(diào)查報(bào)告范文
- 醫(yī)院設(shè)備采購方案(3篇)
- 2025至2030中國智能物流行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報(bào)告
- 2024年武漢市漢陽區(qū)招聘社區(qū)干事考試真題
- 廣告項(xiàng)目方案投標(biāo)文件(技術(shù)方案)
- 北師大版4四年級下冊數(shù)學(xué)期末復(fù)習(xí)試卷(5套)
- 遼寧省大連市甘井子區(qū)2023-2024學(xué)年七年級下學(xué)期期末生物學(xué)試題(原卷版)
- 鐵路行車組織(高職)全套教學(xué)課件
評論
0/150
提交評論