讨论调试程序中的小技巧

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

这样的。有办法定位么?
...全文
267 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)

64,643

社区成员

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

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