在 C++ 中禁用断言宏
Disabling assert macro in C++
我试图以这种方式禁用 C++ 中的断言宏:
#include <stdio.h> /* printf */
#include <assert.h> /* assert */
#define NDEBUG
void print_number(int* myInt) {
assert (myInt != NULL);
printf ("%d\n", *myInt);
}
int main ()
{
int a = 10;
int * b = NULL;
int * c = NULL;
b = &a;
print_number (b);
print_number (c);
return 0;
}
official website says that if I define NDEBUG, all the assert macro will be disable. This way doesn't work。你能告诉我怎么解决吗?
NDEBUG
定义控制 assert
宏的定义,而不是它的扩展。为了使其有效,您需要在 之前定义它 定义宏本身,这发生在 assert.h
.
有两种方法可以实现:
- 将
#define NDEBUG
放在 #include <assert.h>
行之前;或
- 在命令行上定义
NDEBUG
,方法如下:
cc -DNDEBUG main.c
也许您还应该退后一步,考虑一下为什么要尝试禁用断言。毕竟,断言的存在是有原因的,所以除非您 运行 在 40MHz SPARCstation 上,否则您可能不应该禁用它们。
我试图以这种方式禁用 C++ 中的断言宏:
#include <stdio.h> /* printf */
#include <assert.h> /* assert */
#define NDEBUG
void print_number(int* myInt) {
assert (myInt != NULL);
printf ("%d\n", *myInt);
}
int main ()
{
int a = 10;
int * b = NULL;
int * c = NULL;
b = &a;
print_number (b);
print_number (c);
return 0;
}
official website says that if I define NDEBUG, all the assert macro will be disable. This way doesn't work。你能告诉我怎么解决吗?
NDEBUG
定义控制 assert
宏的定义,而不是它的扩展。为了使其有效,您需要在 之前定义它 定义宏本身,这发生在 assert.h
.
有两种方法可以实现:
- 将
#define NDEBUG
放在#include <assert.h>
行之前;或 - 在命令行上定义
NDEBUG
,方法如下:
cc -DNDEBUG main.c
也许您还应该退后一步,考虑一下为什么要尝试禁用断言。毕竟,断言的存在是有原因的,所以除非您 运行 在 40MHz SPARCstation 上,否则您可能不应该禁用它们。