【发布时间】:2010-09-28 04:47:06
【问题描述】:
在阅读了一些关于滥用异常的线程之后(基本上是说如果函数的先决条件不正确,您不想展开堆栈 - 可能表明您的所有内存都已损坏或同样危险的东西)我正在考虑使用 assert () 更频繁。以前我只使用 assert() 作为调试工具,我认为这就是很多 C++ 程序员使用它的方式。我担心我的部分错误处理会被未来某个时候引入运行时构建的 NDEBUG #define 关闭。有没有办法解决这个问题并且其他人对此有疑问(即我应该担心它)吗?
谢谢, 帕特
编辑: 我正在阅读的线程的要点是,如果您的应用程序真的被窃听,那么展开堆栈可能会损坏系统,例如,如果析构函数将某些内容写入文件并且文件句柄已损坏。我不建议使用 assert 进行正常的错误处理。我目前的用例很弱,但看看你的想法:
//check later code won't crash the system
if( buf.length() % 2 )
return false;
// do other stuff that shouldn't affect bufs length
//copy 2 bytes into buf at a time, if length is odd then don't know
//what will happen so use assert to make sure it can't damage anything
assert( !(buf.length() % 2) );
for( i = 0; i != buf.length(); i += 2 )
memcpy( buf + i, data, 2 );
edit2:讨论在这里: http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/80083ac31a1188da
【问题讨论】:
标签: c++ error-handling