宏定义问题
//Testing the allege() macro
#include <iostream>
#include "ALLEGE.h"
#include <fstream>
#define NDEBUG //turn off asserts
using namespace std;
int main()
{
int i = 1;
allege(i,"value must be nonzero");
void* m = malloc(100);
allegemem(m);
ifstream nofile("nofile.xxx");
allegefile(nofile);
return 0;
}
#ifndef ALLEGE_H_INCLUDED
#define ALLEGE_H_INCLUDED
//ALLEGE.H --error checking
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
inline void allege_error(int val,const char *msg)
{
if(!val)
{
fprintf(stderr,"error: %s\n",msg);
#ifdef NDEBUG //turn off debug
exit(1);
#endif
}
}
#define allege(expr,msg) \
{ allege_error((expr)?1:0,msg); \
assert(expr); }
#define allegemem(expr) \
allege(expr,"out of memory")
#define allegefile(expr) \
allege(expr,"could not open file")
#endif // ALLEGE_H_INCLUDED
为什么在主程序中加了#define NDEBUG,但还是会提示assert检查错误?
[解决办法]
宏定义和使用的先后位置顺序要清楚
lz 的使用明明在 define 之前,当然不起作用
#define NDEBUG //turn off asserts#include "ALLEGE.h"#include <fstream>
[解决办法]
NDEBUG只能关闭ASSERT(全部大写)这个宏,不会关闭真正的assert