断言如何真正使您的代码受益? [英] How does assert benefit your code really?

查看:50
本文介绍了断言如何真正使您的代码受益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了打印出来之外,例如


" a.out:p113.c:8:main:断言'0''失败。

中止"


和一个开关选项NDEBUG,还有什么其他的在任何设计,调试/编码和/或测试范围内,assert()都能提供

吗?


你更喜欢这种语言的if语句吗?断言编译器MACRO

预编译器?


-

lovecreatesbeauty

Besides printing out for example

" a.out: p113.c:8: main: Assertion `0'' failed.
Aborted "

and a switch option NDEBUG, what other benefits does assert() provide
in any scope of designing, debugging/coding and/or testing?

Do you prefer the if statement of the language to the assert MACRO of
the precompiler?

--
lovecreatesbeauty

推荐答案



lovecreatesbeauty写道:

lovecreatesbeauty wrote:
除了打印出来之外

a.out:p113.c:8:main:断言0失败。
中止

和切换选项NDEBUG,assert()提供的其他好处是什么?在任何设计,调试/编码和/或测试的范围内?

你更喜欢语言的if语句到预编译器的断言MACRO吗?
Besides printing out for example

" a.out: p113.c:8: main: Assertion `0'' failed.
Aborted "

and a switch option NDEBUG, what other benefits does assert() provide
in any scope of designing, debugging/coding and/or testing?

Do you prefer the if statement of the language to the assert MACRO of
the precompiler?




断言通常用于捕获程序中的逻辑错误,如果

在正确测试后被认为是不存在的(注意断言从不

fires),不需要在你完成的

产品中进行错误检查



assert is usually used to catch logical errors in your program, that if
deemed nonexistant after proper testing (noticing that the assert never
fires), would not require the error checking anymore in your finished
product


lovecreatesbeauty写道:
lovecreatesbeauty wrote:
除了打印出来之外,例如

a.out:p113.c:8:main:断言0失败。
中止

和切换选项NDEBUG,assert()提供的其他好处是什么?在任何设计,调试/编码和/或测试的范围内?

你更喜欢语言的if语句到预编译器的断言MACRO吗?
Besides printing out for example

" a.out: p113.c:8: main: Assertion `0'' failed.
Aborted "

and a switch option NDEBUG, what other benefits does assert() provide
in any scope of designing, debugging/coding and/or testing?

Do you prefer the if statement of the language to the assert MACRO of
the precompiler?




[新手回答]

对我来说,断言()和if()是两个完全不同的东西,每个都使用

完全不同的原因。

#include< assert.h>

int main(void){

assert(argc> ; = 2); / *错误使用assert()* /

if(CHAR_BIT> 8){/ *错误使用if()* /

退出(EXIT_FAILURE);

}

返回0;

}

或者,更可能的例子,让我们说你有一个函数,它将

的所有int'加在一个大小的数组中。在函数描述中,你说
表示数组不能为空。


int sum_array(const int * array,size_t elems){

int x = 0;

assert(array!= NULL);

while(elems--)x + = * array ++;

返回x;

}


当使用NDEBUG定义编译此代码时,将无法检查

到位抓住程序员的错误。


-

如果您通过Google发帖,请阅读< http://cfaj.freeshell.org/ google>



[newbie answering]
To me, assert() and if() are two completely different things, each used
for completely different reasons.

#include <assert.h>
int main(void) {
assert(argc >= 2); /* wrong use of assert() */
if (CHAR_BIT > 8) { /* wrong use of if() */
exit(EXIT_FAILURE);
}
return 0;
}
Or, for a more likely example, let''s say you have a function that sums
all the int''s in an array of some size. In the function description you
say the array must not be empty.

int sum_array(const int * array, size_t elems) {
int x = 0;
assert(array != NULL);
while (elems--) x += *array++;
return x;
}

When this code is compiled with NDEBUG defined there will be no checks
in place to catch programmer''s mistakes.

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>


lovecreatesbeauty写道:
lovecreatesbeauty wrote:
除了打印出来之外,例如

" a.out:p113.c:8:main:断言0失败。
中止

和切换选项NDEBUG,assert()提供的其他好处是什么?在任何设计,调试/编码和/或测试的范围内?

你更喜欢语言的if语句到预编译器的断言MACRO吗?
Besides printing out for example

" a.out: p113.c:8: main: Assertion `0'' failed.
Aborted "

and a switch option NDEBUG, what other benefits does assert() provide
in any scope of designing, debugging/coding and/or testing?

Do you prefer the if statement of the language to the assert MACRO of
the precompiler?




断言可能是你最好的朋友。例如,如果代替:

int

foo(int x,int y)

{/ *返回x和y的某些函数。请记住,x需要

在5到10之间,y必须在15到150之间

或者这将是段错! * /

....

}


你写的:

int

foo(int x,int y)

{

断言(x> 5&& x< = 10);

断言(y> = 15& y< 150);

....

}


您将获得2项主要优惠。

1)消除英语描述中固有的歧义

关于端点是否包含端点,

2)当某些代码被更改以便调用者使用

错误的参数时,你会立即意识到这一点

当你运行时,而不是随机获得段错误和

必须追查导致它的原因。

此外,置位断言使维护者明白

是什么编码器预计。任何时候你期望

是真的,请提出一个断言。如果它失败了,你或者在某个地方有/或
a编码错误,或者你的期望是错误的。

要么很高兴知道。


Assert比这种类型的东西更好,因为当你编译非调试版本时,它会消失
。你可以用你的

if语句中的预编译器包装来实现这一点,但在大多数情况下它在美元上没有吸引力。


这篇关于断言如何真正使您的代码受益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆