讨论调试程序中的小技巧

phoeni_xin 2011-09-20 04:53:19

FILE *pfile = fopen("1.txt","r");
char ch[100];
memset(ch,0,101);
fread(ch,1,100,pfile);


比如类似代码,编译时不报错。

跑起来会有异常。

这时候,在context里面去找异常。

如何快速的定位哪里出错了?请教下。

还一个小问题,调试时,如果出现汇编界面,比如:

7C93AC4A inc dword ptr [eax+10h]
7C93AC4D mov eax,dword ptr [ebp-4]
7C93AC50 and eax,1
7C93AC53 mov dword ptr [ebp-18h],eax
7C93AC56 mov eax,dword ptr [esi]
7C93AC58 inc dword ptr [eax+14h]
7C93AC5B test byte ptr ds:[7FFE02F0h],1
7C93AC62 jne 7C9639C6
7C93AC68 cmp dword ptr [ebp-18h],ebx
7C93AC6B push edi

这样的。有办法定位么?
...全文
292 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanae 2011-09-28
  • 打赏
  • 举报
回复
C++:记得还有static_assert哦~亲
赵4老师 2011-09-26
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 phoeni_xin 的回复:]
记得还有assert哦~亲

assert 用法介绍下啊。亲。
[/Quote]
英语也是一门计算机语言的说。

assert
Evaluates an expression and when the result is FALSE, prints a diagnostic message and aborts the program.

void assert( int expression );

Routine Required Header Compatibility
assert <assert.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

None

Parameter

expression

Expression (including pointers) that evaluates to nonzero or 0

Remarks

The ANSI assert macro is typically used to identify logic errors during program development, by implementing the expression argument to evaluate to false only when the program is operating incorrectly. After debugging is complete, assertion checking can be turned off without modifying the source file by defining the identifier NDEBUG. NDEBUG can be defined with a /D command-line option or with a #define directive. If NDEBUG is defined with #define, the directive must appear before ASSERT.H is included.

assert prints a diagnostic message when expression evaluates to false (0) and calls abort to terminate program execution. No action is taken if expression is true (nonzero). The diagnostic message includes the failed expression and the name of the source file and line number where the assertion failed.

The destination of the diagnostic message depends on the type of application that called the routine. Console applications always receive the message via stderr. In a single- or multithreaded Windows application, assert calls the Windows MessageBox API to create a message box to display the message along with an OK button. When the user chooses OK, the program aborts immediately.

When the application is linked with a debug version of the run-time libraries, assert creates a message box with three buttons: Abort, Retry, and Ignore. If the user selects Abort, the program aborts immediately. If the user selects Retry, the debugger is called and the user can debug the program if Just-In-Time (JIT) debugging is enabled. If the user selects Ignore, assert continues with its normal execution: creating the message box with the OK button. Note that choosing Ignore when an error condition exists can result in “undefined behavior.” For more information, see Using C Run-Time Library Debugging Support.

The assert routine is available in both the release and debug versions of the C run-time libraries. Two other assertion macros, _ASSERT and _ASSERTE, are also available, but they only evaluate the expressiosn passed to them when the _DEBUG flag has been defined.

Example

/* ASSERT.C: In this program, the analyze_string function uses
* the assert function to test several conditions related to
* string and length. If any of the conditions fails, the program
* prints a message indicating what caused the failure.
*/

#include <stdio.h>
#include <assert.h>
#include <string.h>

void analyze_string( char *string ); /* Prototype */

void main( void )
{
char test1[] = "abc", *test2 = NULL, test3[] = "";

printf ( "Analyzing string '%s'\n", test1 );
analyze_string( test1 );
printf ( "Analyzing string '%s'\n", test2 );
analyze_string( test2 );
printf ( "Analyzing string '%s'\n", test3 );
analyze_string( test3 );
}

/* Tests a string to see if it is NULL, */
/* empty, or longer than 0 characters */
void analyze_string( char * string )
{
assert( string != NULL ); /* Cannot be NULL */
assert( *string != '\0' ); /* Cannot be empty */
assert( strlen( string ) > 2 ); /* Length must exceed 2 */
}


Output

Analyzing string 'abc'
Analyzing string '(null)'
Assertion failed: string != NULL, file assert.c, line 24

abnormal program termination


Error Handling Routines | Process and Environment Control Routines

See Also abort, raise, signal, _ASSERT, _ASSERTE, _DEBUG
phoeni_xin 2011-09-26
  • 打赏
  • 举报
回复
记得还有assert哦~亲

assert 用法介绍下啊。亲。
赵4老师 2011-09-23
  • 打赏
  • 举报
回复
mk:@MSITStore:D:\MSDN98\98VS\2052\vccore.chm::/html/_core_setting_breakpoints_when_values_change_or_become_true.htm
Setting Breakpoints When Values Change or Become True
You can set data breakpoints that halt execution when an expression changes value or evaluates to true. The debugger automatically knows whether “changes” or “true” makes sense for the variable or expression you have entered — you don’t need to set this yourself.

You can set a breakpoint on any valid C or C++ expression. Breakpoint expressions can also use memory addresses and register mnemonics. The debugger interprets all constants as decimal numbers unless they begin with '0' (octal) or '0x' (hexadecimal).

This section covers the following topics:

Setting a Breakpoint When a Variable Changes Value


Setting a Breakpoint When an Expression Changes Value


Setting a Breakpoint When an Expression Is True


Setting a Breakpoint On a Variable Outside the Current Scope


Setting a Breakpoint When the Initial Element of an Array Changes Value


Setting a Breakpoint When the Initial Element of an Array Has a Specific Value


Setting a Breakpoint When a Particular Element of an Array Changes Value


Setting a Breakpoint When Any Element of an Array Changes Value


Setting a Breakpoint When Any of the First n Elements of an Array Change Value


Setting a Breakpoint When the Location Value of a Pointer Changes


Setting a Breakpoint When the Value at a Location Pointed to Changes


Setting a Breakpoint When an Array Pointed to by a Pointer Changes


Setting a Breakpoint When the Value at a Specified Memory Address Changes


Setting a Breakpoint When a Register Changes


Setting a Breakpoint When a Register Expression is True
phoeni_xin 2011-09-23
  • 打赏
  • 举报
回复
查看 call stack

如何查看 call stack?从里面获得有效信息呢。
sanae 2011-09-23
  • 打赏
  • 举报
回复
记得还有assert哦~亲
冼鸿文 2011-09-23
  • 打赏
  • 举报
回复
回去慢慢学习
淡然一笑 2011-09-23
  • 打赏
  • 举报
回复
memset(ch,0,sizeof(ch))

让编译器自己去获取数组长度……
Tiger-3D 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 tony2278 的回复:]
我都是设断点·
[/Quote]

目前也是,那別的什麽工具都還沒有接觸過!
就想叫yoko 2011-09-22
  • 打赏
  • 举报
回复
查看 call stack
cocat 2011-09-22
  • 打赏
  • 举报
回复
最简单应该还是断点,然后单步调试~
leer168 2011-09-21
  • 打赏
  • 举报
回复
寄希望于自动定位貌似不中
只能自己考虑周全,多加保护了吧
luciferisnotsatan 2011-09-21
  • 打赏
  • 举报
回复
1 没判断fopen是否成功
2 数组100大小,却memset了101
char ch[100];
memset(ch,0,101);
mstlq 2011-09-21
  • 打赏
  • 举报
回复
调试前先用用cppcheck吧
tony2278 2011-09-21
  • 打赏
  • 举报
回复
然后单步运行到异常的地方,然后再分析问题
5t4rk 2011-09-21
  • 打赏
  • 举报
回复
要不就是借助工具
要不就是手动
我还是喜欢手动

跟踪调试 查看内存

再看反汇编
tony2278 2011-09-21
  • 打赏
  • 举报
回复
我都是设断点·
stendson 2011-09-21
  • 打赏
  • 举报
回复
F5 F9 F10 F11 shift+F11 +watch window + memory window
phoeni_xin 2011-09-21
  • 打赏
  • 举报
回复
嗯。还有吗?
赵4老师 2011-09-20
  • 打赏
  • 举报
回复
崩溃的时候进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。
在某个调用历史上双击可以自动跳转到调用的源代码处或汇编代码处。
加载更多回复(6)
1. C 语言的指针和内存泄漏 5 2. C语言难点分析整理 10 3. C语言难点 18 4. C/C++实现冒泡排序算法 32 5. C++指针和引用的区别 35 6. const char*, char const*, char*const的区别 36 7. C可变参数函数实现 38 8. C程序内存组成部分 41 9. C编程拾粹 42 10. C语言实现数组的动态增长 44 11. C语言的位运算 46 12. 浮点数的存储格式: 50 13. 位域 58 14. C语言函数二维数组传递方法 64 15. C语言复杂表达式的执行步骤 66 16. C语言字符串函数大全 68 17. C语言宏定义技巧 89 18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区溢出方法 126 25. C语言高效编程秘籍 128 26. C运算符优先级口诀 133 27. do/while(0)的妙用 134 28. exit()和return()的区别 140 29. exit子程序终止函数与return的差别 141 30. extern与static存储空间矛盾 145 31. PC-Lint与C\C++代码质量 147 32. spirntf函数使用大全 158 33. 二叉树的数据结构 167 34. 位运算应用口诀和实例 170 35. 内存对齐与ANSI Cstruct内存布局 173 36. 冒泡和选择排序实现 180 37. 函数指针数组与返回数组指针的函数 186 38. 右左法则- 复杂指针解析 189 39. 回车和换行的区别 192 40. 堆和堆栈的区别 194 41. 堆和堆栈的区别 198 42. 如何写出专业的C头文件 202 43. 打造最快的Hash表 207 44. 指针与数组学习笔记 222 45. 数组不是指针 224 46. 标准C字符串分割的方法 228 47. 汉诺塔源码 231 48. 洗牌算法 234 49. 深入理解C语言指针的奥秘 236 50. 游戏外挂的编写原理 254 51. 程序实例分析-为什么会陷入死循环 258 52. 空指针究竟指向了内存的哪个地方 260 53. 算术表达式的计算 265 54. 结构体对齐的具体含义 269 55. 连连看AI算法 274 56. 连连看寻路算法的思路 283 57. 重新认识:指向函数的指针 288 58. 链表的源码 291 59. 高质量的子程序 295 60. 高级C语言程序员测试必过的十六道最佳题目+答案详解 297 61. C语言常见错误 320 62. 超强的指针学习笔记 325 63. 程序员之路──关于代码风格 343 64. 指针、结构体、联合体的安全规范 346 65. C指针讲解 352 66. 关于指向指针的指针 368 67. C/C++ 误区一:void main() 373 68. C/C++ 误区二:fflush(stdin) 376 69. C/C++ 误区三:强制转换 malloc() 的返回值 380 70. C/C++ 误区四:char c = getchar(); 381 71. C/C++ 误区五:检查 new 的返回值 383 72. C 是 C++ 的子集吗? 384 73. C和C++的区别是什么? 387 74. 无条件循环 388 75. 产生随机数的方法 389 76. 顺序表及其操作 390 77. 单链表的实现及其操作 391 78. 双向链表 395 79. 程序员数据结构笔记 399 80. Hashtable和HashMap的区别 408 81. hash 表学习笔记 410 82. C程序设计常用算法源代码 412 83. C语言有头结点链表的经典实现 419 84. C语言惠通面试题 428 85. C语言常用宏定义 450

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧