主题:vC6.0 的 debug 模式下运行时总是提示如下错误:
写了一个非常简单的程序,但是在 vC6.0 的 debug 模式下运行时总是提示如下错误:
Debug Assertion Failed!
Program: ****.exe
File: dbgheap.c
line: 1044
Expression: _CrtIsValidHeapPoint(pUserData)
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts
(1)
#ifndef Cexception_H
#define Cexception_H
#include <iostream>
using namespace std;
class Cexception
{
public:
Cexception(const char *Reason=0);
~Cexception();
const char *getReason() const;
char *reason;
};
#include "Cexception.cpp"
#endif
(2)#include "Cexception.h"
#include <iostream>
using namespace std;
Cexception::Cexception(const char *Reason)
{
if(reason!=NULL)
{
delete reason;
}
else
{
int i=0;
while(Reason[i++]!='\0'){};
cout<<"i="<<i<<endl;
reason=new char[i+1];
i=0;
while(Reason[i]!='\0')
{
reason[i]=Reason[i];
i=i+1;
}
reason[i]='\0';
}
}
Cexception::~Cexception()
{
if(reason!=NULL)
{
delete reason;
}
}
const char *Cexception::getReason() const
{
return reason;
}
(3)
#include "Cexception.h"
#include <iostream>
using namespace std;
void fun()
{
cout<< "在fun 中抛掷Cexception 类异常。"<<endl;
throw Cexception ("FUN");
}
int main()
{cout << "在main 函数中。"<<endl;
try
{
cout << "在try 模块中,调用fun()。"<<endl;
fun();
}
catch(Cexception e)
{
cout<<"在catch 模块异常处理程序中。"<<endl;
cout<<"捕获到Cexception 类型异常:";
cout<<e.getReason()<<endl;
}
cout<<"回到main 函数。"<<endl;
return 0;
}