Welcome

首页 / 软件开发 / C++ / VC++调试方法和技巧

VC++调试方法和技巧2011-04-07非凡便于调试的代码风格:

不用全局变量

所有变量都要初始化,成员变量在构造函数中初始化

尽量使用const

详尽的注释

VC++编译选项:

总是使用/W4警告级别

在调试版本里总是使用/GZ编译选项,用来发现在Release版本中才有的错误

没有警告的编译:保证在编译后没有任何警告,但是在消除警告前要进行仔细检查

调试方法:

1、使用 Assert(原则:尽量简单)

assert只在debug下生效,release下不会被编译。

例子:

char* strcpy(char* dest,char* source)
{
assert(source!=0);
assert(dest!=0);
char* returnstring = dest;

while((*dest++ = *source++)!= ‘’)
{
;
}
return returnstring;
}

2、防御性的编程

例子:

char* strcpy(char* dest,char* source)
{
if(source == 0)
{
assert(false);
reutrn 0;
}
if(dest == 0)
{
assert(false);
return 0;
}
char* returnstring = dest;
while((*dest++ = *source++)!= ‘’)
{
;
}
return returnstring;
}