程序经常会出这个问题或者那个问题, 遇到问题, 不要总是第一时间问别人, 要多学会自己去调试。 在本文中, 我来介绍“极小化程序调试法”, 这个名字是我自己取的, 其实呢, 它类似于我们经常说的的二分查找。
我们先来看一段程序(环境是VC++6.0), 这段程序是我某次在家玩代码的时候写的:
#include <windows.h>
#include <iostream>
using namespace std;
// 对象的id值
typedef enum
{
ErrorId = -1,
IntegerId = 1,
PointId = 2,
RectangeId = 3,
}ObjectID;
// 基类
struct Basic
{
ObjectID id;
virtual Basic *copy() = 0; // 纯虚函数
};
// 整数类
struct Integer : public Basic
{
int a;
Basic *copy()
{
Integer *p = new Integer;
p->a = ((Integer*)this)->a;
p->id = ((Integer*)this)->id;
return p;
}
};
// 点类
struct Point : public Basic
{
int x;
int y;
Basic *copy()
{
Point *p = new Point;
p->x = ((Point*)this)->x;
p->y = ((Point*)this)->y;
p->id = ((Point*)this)->id;
return p;
}
};
// 矩形类
struct Rectangle : public Basic
{
Point point;
int width;
int height;
Basic *copy()
{
Rectangle *p = new Rectangle;
p->point.x = ((Rectangle*)this)->point.x;
p->point.y = ((Rectangle*)this)->point.y;
p->width = ((Rectangle*)this)->width;
p->height = ((Rectangle*)this)->height;
p->id = ((Rectangle*)this)->id;
return p;
}
};
// 抽象对象的共同点, 构造成新的结点, 便于链接
typedef struct node
{
node *next;
Basic *pBasic;
}Node;
Node *head = NULL; // 指向第一结点(采用不带头结点的链表)
// 往链式消息队列中塞消息
Node *addToMsgQueue(Basic* pb)
{
Node *pn = new Node;
Node *qn = NULL;
Basic *p = pb->copy(); // 多态性
if(NULL == head)
{
head = pn;
}
else
{
qn = head;
while(NULL != qn->next)
{
qn = qn->next;
}
qn->next = pn;
}
pn->pBasic = p; // 千万别忘记啊
pn->next = NULL; // 千万别忘记啊
return head;
}
// 从链式消息队列中取出消息(结点)
Node *getMsgFromQueue()
{
if(NULL == head)
{
return NULL;
}
Node *pn = head;
head = head->next;
return pn;
}
// 线程函数
DWORD WINAPI ThreadFun(LPVOID pM)
{
Node *p = NULL;
// 从消息队列中取出消息
while(1)
{
p = getMsgFromQueue();
if(NULL == p)
{
Sleep(100);
continue;
}
// 对指针进行还原
switch(p->pBasic->id)
{
case IntegerId:
{
cout << ((Integer*)(p->pBasic))->a << endl;
break;
}
case PointId:
{
cout << ((Point *)(p->pBasic))->x << endl;
cout << ((Point *)(p->pBasic))->y << endl;
break;
}
case RectangeId:
{
cout << ((Rectangle *)(p->pBasic))->point.x << endl;
cout << ((Rectangle *)(p->pBasic))->point.y << endl;
cout << ((Rectangle *)(p->pBasic))->width << endl;
cout << ((Rectangle *)(p->pBasic))->height << endl;
break;
}
default:
{
break;
}
}
}
return 0;
}
// 主线程
int main()
{
HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL);
CloseHandle(handle);
// 定义三个对象并赋值
Integer i;
Point po;
Rectangle rect;
i.id = IntegerId;
po.id = PointId;
rect.id = RectangeId;
i.a = 11;
po.x = 22;
po.y = 33;
rect.point.x = 44;
rect.point.y = 55;
rect.width = 66;
rect.height = 77;
// 塞入消息队列
while(1)
{
addToMsgQueue(&i);
addToMsgQueue(&po);
addToMsgQueue(&rect);
Sleep(2000);
}
return 0;
}
我估计, 如果只凭肉眼, 应该没有人能看出上面程序有什么编译错误, 如果能看出来, 要么是个天才, 要么是个废材。
好吧, 我们来编译一下:发现错误如下:
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
main.cpp
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(162) : error C2059: syntax error : ')'
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(163) : error C2059: syntax error : ')'
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(164) : error C2059: syntax error : ')'
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(165) : error C2059: syntax error : ')'
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(188) : error C2146: syntax error : missing ';' before identifier 'rect'
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(188) : warning C4551: function call missing argument list
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(188) : error C2065: 'rect' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(192) : error C2228: left of '.id' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(198) : error C2228: left of '.point' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(198) : error C2228: left of '.x' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(199) : error C2228: left of '.point' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(199) : error C2228: left of '.y' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(200) : error C2228: left of '.width' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(201) : error C2228: left of '.height' must have class/struct/union type
Error executing cl.exe.
main.obj - 13 error(s), 1 warning(s)
遇到错误, 我们可以参考上述错误信息, 不过, 上述错误信息的提示有点古怪, 不太好分析。 不要着急,我们将程序极小化为:
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
return 0;
}
编译连接运行都ok, 好的, 我们终于构造出了正确的极小化程序, 然后就往上慢慢添加东西, 看什么时候出问题。 如果程序加多了, 出了问题, 那就继续对错误程序进行瘦身, 这样反复二分地搞, 肯定能找到错误的地方。 事实上, 我就是这么调试出上述程序的问题的。
有兴趣的朋友, 可以尝试调试一下上述程序, 肯定会有所收获, 在本文中, 我就不公布答案了。
在实际的软件开发中, 经常需要采用类似的“极小化程序调试法”来调试程序, 把错误锁定在一个范围, 反复二分, 反复对比, 必定能找出问题。 当然, 要注意, 所谓的极小是尽可能小, 而不是说一定要最小(简化到只有main), 在实际代码中, 怎么可能仅仅简化到main呢?
OK, 本文仅仅提供一种常见的调试思路, 关键在于多多实践。