




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C++编程的调试技巧C++编程的调试技巧C++编程的调试技巧主要内容VC++集成开发环境调试方法如何独立解决问题编程规范C++编程的调试技巧一、集成开发环境的熟悉C++编程的调试技巧保存、全部保存打开最近的工程C++编程的调试技巧C++编程的调试技巧C++编程的调试技巧给工程中添加文件C++编程的调试技巧查找文件在指定目录下的某类型文件中搜索所有包含某字符串的文件C++编程的调试技巧Debug:带有调试信息,文件大。Release:没有调试信息,不能调试,文件小C++编程的调试技巧切换Debug模式和Release模式C++编程的调试技巧比较文件大小C++编程的调试技巧编译(ctrl+F7):编译源文件到目标文件构建(F7):链接目标文件和库函数为可执行文件,如无目标文件则先生成清洁:删掉debug或者release版本的所有中间文件和可执行文件调试 单步执行(F10):单步执行,遇到函数调用时把其当作一条语句执行 深入函数的单步执行(F11):单步执行,遇到函数调用是深入到其内部 执行到光标处(ctrl+F10):一次执行完光标前的所有语句,并停到光标处 跳出(shift+F11):执行完当前函数的所有剩余代码,并从函数跳出 重新开始调试(ctrl+shift+F5):重新开始调试过程 结束调试(shift+F5):执行完程序的剩余部分,结束调试 设置/取消断点(F9):在某一行设置和取消断点C++编程的调试技巧C++编程的调试技巧当前执行的语句堆栈的内容:函数调用关系从这里可以查看内存数据程序执行过程中的一些变量会显示在这里著名的watch窗口:看变量表达式、地址等各种信息寄存器当前值C++编程的调试技巧二、调试方法C++编程的调试技巧树立正确的编程方法论没有解决不了的问题,树立编程信心按照正确的方法来编程从实践中体会解决问题的思路、编程的思想熟练掌握语法,数据结构,算法、模式,底层环境按照编程规范来编写代码充分利用各种资源:调试器msdn网络搜索引擎C++编程的调试技巧常用调试手段利用编译器的输出信息排除错误利用调试器的调试功能 单步、runtocursor、断点、条件断点、查看堆栈、 利用watch窗口查看变量、表达式的值分段调试法、增量调试法利用flush人为刷新缓冲区修改输出信息,增加标志位利用输出语句打印调试信息通过堆栈观察函数调用情况综合程序调试方法注释的技巧、条件编译C++编程的调试技巧几个简单例子C++编程的调试技巧#include<iostream.h>#include<iomanip.h>voidmain(){ cout<<setw(10)<<"十进制"<<setw(10)<<"二进制"<<setw(10)<<"八进制“
<<setw(10)<<"十六进制"<<endl; for(inti=1;i<=156;i++){cout<<setw(10)<<dec<<i<<""; intd; d=i;for(intj=128;j>=1;j/=2){if(d/j==1) cout<<"1";if(d/j==0) cout<<"0";d=d%j;}cout<<setw(10)<<oct<<i<<setw(10)<<hex<<i<<endl;}}一个编程风格非常糟糕的例子:C++编程的调试技巧/*""""************************************************************SystemName:foreduction(NOTRANSFERRING)*:e05b.c*Contents:embeddedClanguageentrancecourse*:exercise5B:programusingpointer*:乮passone-dimensionalarraytofunction)*Model:forOAKS8-LCDBoard*CPU:R8C/Tinyseries*Compiler:NC30WA(V.5.30Release1)*OS:notbeused*Programmer:RENESASSemiconductorTrainingCenter*Note:forOAKS8-R5F21114FP(R8C/11group,20MHz)*****************************************************************************COPYRIGHT(C)2004RENESASTECHNOLOGYCORPORATION*ANDRENESASSOLUTIONSCORPORATIONALLRIGHTSRESERVED*****************************************************************************History:*""END""*******************************************************//*includefile*/#include"defs.h" /*definecommonsymbol */#include"oaks8lib.h" /*forfunctiontodealwithOAKS8-LCDBoardperipheral *//*definefunctionprototype*/intmain(void);staticvoidget_string(unsignedchar*message,unsignedchar*buff); /*inputstring */staticintget_strlength(unsignedchar*str); /*getstringlength*/staticvoidprint_dec(unsignedchar*message,intdec); /*displaydecimalnumberinLCD *//*""FUNCCOMMENT""**************************************************************ID:1.0*functionname:main*function:callfunctiontogetstringlength,displayresult*parameter:none*return:result0:normalend*functionused:get_strlength*notice:none*History:*""FUNCCOMMENTEND""********************************************************/intmain(void){ unsignedcharbuff[10+1]; /*definevariabletosavestringinputfromkeymatrix */ intcount; /*definevariableforsavenumberofcharacter*/
while(1){ /*inputstringfromkeymatrix*/ get_string("Str=",buff);
/*callfunctiontogetstringlength*/ /*passaddressofarraysavingstring,getstringlength*/ count=get_strlength(buff);
/*displaystringlengthinLCD*/ print_dec("Len=",count); } return0;}/*""FUNCCOMMENT""**************************************************************ID:1.1*functionname:get_string*function:inputstringfromkeymatrix*parameter:message:pointertoinputwaitingmessage*:buff:pointertospacesavingstringinput*return:none*functionused:none*notice:none*History:*""FUNCCOMMENTEND""********************************************************/staticvoidget_string(unsignedchar*message,unsignedchar*buff) /*definefunctiontoinputstring */{ LCD__puts(message); /*displaymessage */ SW__gets(buff); /*inputstringfromkeymatrix */ LCD__puts(buff); /*displaystringinput */ LCD__putchar('\n'); /*return */}/*""FUNCCOMMENT""**************************************************************ID:1.2*functionname:get_strlength*function:referstringusingpointerpassedasargument,returnlength*parameter:str:addressofstringtocountlength*return:length*functionused:none*notice:none*History:*""FUNCCOMMENTEND""********************************************************/staticintget_strlength(unsignedchar*str) /*definefunctiontogetstringlength */{ intlen=0; /*defineandinitializevariableforstringlength*/
/*refereverycharacterinone-dimensionalarraybuffpointedby*/ /*pointerstr,countupuntildetect('\0')atendofstring*/ while('\0'!=*str++){ len++; /*countnumberofcharacter */ }
returnlen; /*returnlength */}/*""FUNCCOMMENT""**************************************************************ID:1.3*functionname:print_dec*function:displaydecimalnumberinLCD*parameter:message:pointertoinputwaitingmessage*:dec:decimalnumbertobedisplayed*return:none*functionused:none*notice:none*History:*""FUNCCOMMENTEND""********************************************************/staticvoidprint_dec(unsignedchar*message,intdec) /*definefunctiontodisplaydecimalnumberinLCD */{ LCD__puts(message); /*displaymessage */ LCD__putdec(dec); /*displaydecimalnumberinLCD */ LCD__putchar('\n'); /*return */}/*****************************************************************************endoffile******************************************************************************/一个编程风格良好的例子:C++编程的调试技巧结论1永远不要写过长的语句,应该让代码尽可能简单;永远不要把两条语句写在一行中,以便于调试。C++编程的调试技巧#include<iostream.h>voidmain(){ floatnum1,num2,outcome; charop,a; do {cout<<"puttwonumbers:"<<endl; cin>>num1>>num2>>endl; cout<<"enteritsoperater:"<<endl; cin>>op>>endl; 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:cout<<"theycannotbework\n"; } cout<<"theoutcomeis"<<outcome<<endl; } elsecout<<"ocannotasadivider\n"; cout<<"doyouwanttocontinue(y/n)\n"; cin>>a>>endl; }while(a=='y');}两个数的算术运算C++编程的调试技巧//如果输入字符a,则显示1,否则程序结束#include<iostream.h>intf(){ charc; cout<<"请输入a或其他字符"<<endl; cin>>c; if(c=='a') return1; else ;}intmain(){ cout<<f()<<endl; return0;
}
根据输入给出输出C++编程的调试技巧结论2要尽可能的熟悉语法知识充分利用编译器提供的信息编程过程中要细心思路要清晰,没把握时,要先画流程图,然后再编码C++编程的调试技巧#include<iostream.h>intmain(){ doublea,b,c,d; cout<<"shurusanjiaoxingde3bian\n"; cin>>a>>b>>c; if(a==b&&b==c) { cout<<"dengbiansanjiaoxing\n"; d=1.732*a*a/4; cout<<d<<endl; } if(c*c==a*a+b*b||a*a==b*b+c*c||b*b==a*a+c*c) { cout<<"zhijiaosanjiaoxing\n"; d=(a*b/2||a*c/2||b*c/2); cout<<d<<endl; } return0;}判断三角形的形状C++编程的调试技巧结论3要掌握编程的本质: 是由程序员控制机器,使其按照我们的思路运转,机器本身没有智能。C++编程的调试技巧#include<iostream.h>voidmain(){ doubleinteger1,integer2,e,f,g,h; charop,a; cin<<a while(a='y') { cout<<"inputanop:"; cin>>op; cout<<"integer1is:"; cin>>integer1; cout<<"integer2is:"; cin>>integer2;
switch(op). { e=integer1+integer2; f=integer1-integer2; g=integer1*integer2; h=integer1/integer2; case'+':cout<<e; break; case'-':cout<<f; break; case'*':cout<<g; break; case'/':cout<<h; break; } cout<<"Doyouwanttocontinue(Y/Nory/n)?"; }}两个数的算术运算C++编程的调试技巧#include<iostream.h>voidmain(){ doubleinteger1,integer2,e,f,g,h; charop,a; cin<<a //分号、<< while(a=‘y’) //等号
{ cout<<"inputanop:"; cin>>op; cout<<"integer1is:"; cin>>integer1; cout<<"integer2is:"; cin>>integer2;
switch(op). //点
{ e=integer1+integer2; //这段代码执行不到
f=integer1-integer2; g=integer1*integer2; h=integer1/integer2; case'+':cout<<e; break; case'-':cout<<f; break; case'*':cout<<g; break; case'/':cout<<h; break; } cout<<“Doyouwanttocontinue(Y/Nory/n)?”; //标记控制重复
}}两个数的算术运算C++编程的调试技巧结论4编译器不一定能给出确切的错误信息,要合理的“猜测”出错信息通过单步运行的方式加深我们对程序执行流程的理解。单步执行是最重要的调试手段!C++编程的调试技巧#include<iostream.h>intmain(){ chartype=s; while(type!='!') { cout<<"请输入一个字符:"<<endl; cin>>type; if(type>=65&&type<=90) cout<<"输入的字符为大写字母!"<<endl; if(type>=97&&type<=122) cout<<"输入的字符为小写字母!"<<endl; if(type>=48&&type<=57) cout<<"输入的字符为数字!"<<endl; else { cout<<"输入的字符为其他字符!"<<endl; } cout<<"!toend!"<<endl; } return0; } 判断一个字符是什么类别另一个通过单步执行调试的例子C++编程的调试技巧#include<iostream.h>intmain(){ chartype=s; while(type!='!') { cout<<"请输入一个字符:"<<endl; cin>>type;
if(type>=65&&type<=90) cout<<"输入的字符为大写字母!"<<endl; if(type>=97&&type<=122) cout<<"输入的字符为小写字母!"<<endl; if(type>=48&&type<=57) cout<<"输入的字符为数字!"<<endl; else { cout<<"输入的字符为其他字符!"<<endl; } cout<<"!toend!"<<endl;
} return0; } 判断一个字符是什么类别通过单步执行可以搞清程序的执行线路,以发现错误点C++编程的调试技巧#include<iostream.h>intmain(){ intm; cout<<"请输入一个正整数:"<<endl; cin>>m; for(inti=1;i<=m-1;i++) { m=m*i; } cout<<"阶乘为:"<<m<<endl; return0;}求阶乘C++编程的调试技巧#include<iostream.h>intmain(){ intm; cout<<"请输入一个正整数:"<<endl; cin>>m; for(inti=1;i<=m-1;i++) {
m=m*i; //循环体内修改了循环条件
} cout<<"阶乘为:"<<m<<endl; return0;}求阶乘通过单步执行可以监视变量的变化,当变量与预期不一致是,即找到了错误点C++编程的调试技巧#include<iostream.h>intmain(){ intm; cout<<"请输入一个正整数:"<<endl; cin>>m; intn=m; for(inti=1;i<=n-1;i++) { m=m*i; } cout<<"阶乘为:"<<m<<endl; return0;}求阶乘C++编程的调试技巧结论5通过单步执行可以发现程序运行的轨迹通过单步执行可以随时监视变量的值,无论何时发现变量值与我们的预期不一致时,即找到了错误点C++编程的调试技巧单步调试方法前提:debug版本可执行程序,build正确进入调试状态F10为单步调试,要深入到子函数中单步调试,应在函数调用语句处按F11单步调试应该结合watch窗口监视变量值的变化C++编程的调试技巧稍复杂的例子C++编程的调试技巧#include<iostream.h>voidmain(){ for(inti=1;i<=10;i++) { for(intj=1;j<=i;j++) cout<<'*'<<flush; cout<<endl; } cout<<endl; for(intt=1;t<=10;t++) { for(intk=1;k<=11-t;k++) cout<<'*'; cout<<endl; } cout<<endl; for(ints=1;s<=10;s++) { for(intm=1;m<s-1;m++) cout<<''; for(intn=10;n>=11-s;n--) cout<<'*'; cout<<endl; } cout<<endl; for(intz=1;z<=10;z++) { for(intp=9;p<=10-z;p--) cout<<''; for(intq=1;q<=z;q++) cout<<'*'; cout<<endl; } cout<<endl;}输出星号C++编程的调试技巧#include<iostream.h>voidmain(){ for(inti=1;i<=10;i++) { for(intj=1;j<=i;j++) cout<<'*'<<flush; cout<<endl; } cout<<endl; for(intt=1;t<=10;t++) { for(intk=1;k<=11-t;k++) cout<<'*'; cout<<endl; } cout<<endl; for(ints=1;s<=10;s++) { for(intm=1;m<s-1;m++) cout<<''; for(intn=10;n>=11-s;n--) cout<<'*'; cout<<endl; } cout<<endl; for(intz=1;z<=10;z++) { for(intp=9;p<=10-z;p--) cout<<''; for(intq=1;q<=z;q++) cout<<'*'; cout<<endl; } cout<<endl;}输出星号分析:程序比较长,输出怪异,一时难以发现出错地点,所以采用化整为零、化繁为简的分段调试法C++编程的调试技巧#include<iostream.h>voidmain(){ for(inti=1;i<=10;i++) { for(intj=1;j<=i;j++) cout<<'*'<<flush; cout<<endl; } cout<<endl; for(intt=1;t<=10;t++) { for(intk=1;k<=11-t;k++) cout<<'*'; cout<<endl; } cout<<endl; for(ints=1;s<=10;s++) { for(intm=1;m<s-1;m++) cout<<''; for(intn=10;n>=11-s;n--) cout<<'*'; cout<<endl; } cout<<endl; for(intz=1;z<=10;z++) { for(intp=9;p<=10-z;p--) cout<<''; for(intq=1;q<=z;q++) cout<<'*'; cout<<endl; } cout<<endl;}输出星号使用到的调试方法:分段调试单步F10调试runtocursor强制输出(flush)用watch窗口观察表达式的值C++编程的调试技巧#include<iostream.h>voidmain(){ for(inti=1;i<=10;i++) { for(intj=1;j<=i;j++) cout<<'*'<<flush; cout<<endl; } cout<<endl; for(intt=1;t<=10;t++) { for(intk=1;k<=11-t;k++) cout<<'*'; cout<<endl; } cout<<endl; for(ints=1;s<=10;s++) { for(intm=1;m<s;m++) cout<<''<<flush; for(intn=1;n<12-s;n++) cout<<'*'<<flush; cout<<endl; } cout<<endl; for(intz=1;z<=10;z++) { for(intp=9;p>=z;p--) cout<<''<<flush; for(intq=1;q<=z;q++) cout<<'*'<<flush; cout<<endl; } cout<<endl;}
调试后的程序,改正了循环中的逻辑问题,另外,本程序存在变量定义过多的缺点C++编程的调试技巧#include<iostream.h>voidmain(){ for(inti=1;i<=10;i++) { for(intj=1;j<=i;j++) cout<<'*'<<flush; cout<<endl; } cout<<endl; for(intt=1;t<=10;t++) { for(intk=1;k<=11-t;k++) cout<<'*'; cout<<endl; } cout<<endl; for(ints=1;s<=10;s++) { for(intm=1;m<s-1;m++) cout<<''; for(intn=10;n>=11-s;n--) cout<<'*'; cout<<endl; } cout<<endl; for(intz=1;z<=10;z++) { for(intp=9;p<=10-z;p--) cout<<''; for(intq=1;q<=z;q++) cout<<'*'; cout<<endl; } cout<<endl;}输出星号对错误程序的进一步分析:为什么会出现黑屏?错误原因在哪里?可通过修改源代码,设置标志位来验证C++编程的调试技巧结论6分段调试:可以有效降低调试难度,快速定位错误的大致范围,“粗调”单步F10调试:找到可疑点后,逐行执行可疑点附近的代码,对错误“精调”runtocursor:使程序快速运行到可疑点后暂停,克服F10运行速度慢的缺点强制输出(flush):增加有参考价值的信息watch窗口:可观察变量或表达式的值,配合F10进行单步“精调”C++编程的调试技巧小技巧在循环体内,每执行一次runtocursor,就相当于完整的执行了一遍循环体,经常用来替代多次执行F10。C++编程的调试技巧//例求阶乘#include<iostream.h>intFactorial(int);voidmain(){ intk; cout<<"ComputeFactorial(k),Pleaseinputk:"; cin>>k; cout<<k<<"!="<<Factorial(k)<<endl;}intFactorial(intn){ if(n==0) return1; else returnn*Factorial(n-1);}分析程序执行流程求阶乘C++编程的调试技巧C++编程的调试技巧//例求阶乘#include<iostream.h>intFactorial(int);voidmain(){ intk; cout<<"ComputeFactorial(k),Pleaseinputk:"; cin>>k; cout<<k<<"!="<<Factorial(k)<<endl;}intFactorial(intn){ if(n==0) return1; else{intm=n*Factorial(n-1);returnm;}}简化return语句求阶乘C++编程的调试技巧//例求阶乘#include<iostream.h>intFactorial(int);voidmain(){ intk; cout<<"ComputeFactorial(k),Pleaseinputk:"; cin>>k; cout<<k<<"!="<<Factorial(k)<<endl;}intFactorial(intn){ if(n==0) return1; else{cout<<"theparameterofthiscallis:"<<n<<endl;intm=n*Factorial(n-1);cout<<"lastcallgobackhere:"<<n<<endl;returnm;}}增加调试输出信息求阶乘C++编程的调试技巧结论7F11可以深入到函数内部单步执行,F11和F10结合起来使用可以调试多函数的程序。stepover可以从当前函数中跳出到调用点观察堆栈能看到函数之间的调用关系在适当的位置插入输出语句,有助于了解程序的执行流程,是一种十分重要的调试手段C++编程的调试技巧小技巧单步调试时,如果某条语句不是调用自定义的函数,那么不要按F11,尤其是本行包含了coutC++编程的调试技巧//例求菲波那契数列的第五项#include<iostream.h>intFibonacci(intn){ if(n<=2) return1; else returnFibonacci(n-1)+Fibonacci(n-2);}voidmain(){ cout<<Fibonacci(5)<<endl;} 求菲波那契数列第五项请大家注意观察堆栈的变化C++编程的调试技巧综合实例C++编程的调试技巧#include<iostream.h>#include<math.h>#include<iomanip.h>intmain(){ intm,i,k,n=0; boolprime=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) { cout<<setw(5)<<m; n++; } if(n%10==0) cout<<endl; } cout<<endl<<"thetotalnumberis:"<<n<<endl; return0;}求100-200间的素数C++编程的调试技巧#include<iostream.h>#include<math.h>#include<iomanip.h>intmain(){ intm,i,k,n=0; boolprime=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) { cout<<setw(5)<<m; n++; } if(n%10==0) cout<<endl; } cout<<endl<<"thetotalnumberis:"<<n<<endl; return0;}求100-200间的素数用断点来代替runtocursorC++编程的调试技巧在光标处设置断点F9运行程序至断点处F5C++编程的调试技巧光标放到第23行设置条件断点C++编程的调试技巧第23行的有效断点第25行的无效断点C++编程的调试技巧
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- TY/T 1112-2024冰球赛事转播制作规程
- 课题申报书培训反思
- 河南高中课题申报书范例
- 课题申报书活页要盖章吗
- 课题申报书撰写注意点
- 全国规划办课题申报书
- 怎样申报课题申报书
- 幼师申报书课题怎么写
- 厂房土地回收合同范例
- 课题申报评审书范文
- 大学生职业素养训练(第六版)课件 第十二单元养成友善品格
- 哲学与人生 第二课 树立科学的世界观2.1
- 传感器技术-武汉大学
- 初中数学建模研究报告
- 岩石破碎型泥水平衡顶管施工工法
- 人教A版(2019)高中数学选择性必修第二册 《数列的相关概念》教学设计
- 医疗信息共享与互联网医疗管理制度
- 虚劳中医护理方案
- 2024新版有限空间作业安全大培训
- 九年级语文下册-【《孔乙己》课后习题参考答案】
- 【美妆产品的直播带货营销策略探究:以花西子彩妆为例12000字(论文)】
评论
0/150
提交评论