写我自己的断言()的帮助 [英] help for writing my own assert()

查看:62
本文介绍了写我自己的断言()的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


有人能告诉我assert()函数的工作原理吗?我需要开发

我自己的assert()函数,而不是使用

assert.h文件中定义的函数。如果有人能在assert.h头文件中向我展示assert()函数的实际源代码,那就太棒了。


我需要写一下断言当

表达式错误时打印出错误信息的函数。例如,


int a = 2;

int b = 3;

ASSERT(a == b);


谢谢,

Priyanka

Hi there,

Can anyone show me how the assert() function works ? I need to develop
my own assert() function instead of using the one defined in the
assert.h file. It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.

I need to write the assert function that prints out error message when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);

Thank you,
Priyanka

推荐答案

priyanka写道:
priyanka wrote:

你好,


有谁能告诉我assert()函数是如何工作的?我需要

开发

我自己的assert()函数,而不是使用

assert.h文件中定义的函数。如果有人能在assert.h头文件中向我展示assert()函数的实际源代码,那就太棒了。


我需要写一下断言打印出错误信息的函数



表达式错误时。例如,


int a = 2;

int b = 3;

ASSERT(a == b);


谢谢,

Priyanka
Hi there,

Can anyone show me how the assert() function works ? I need to
develop
my own assert() function instead of using the one defined in the
assert.h file. It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.

I need to write the assert function that prints out error message
when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);

Thank you,
Priyanka



#define ASSERT(xy,MSG)做\\ \\ b
{\

if(!(xy)))\

{\

fprintf (stderr,Errormessage:%s,

MSG); \

} \

}而(0)


使用:ASSERT(a == b,我给你的消息)


你可以在fprintf之后插入对assert的调用。或者简单地

退出(EXIT_FAILURE)......


-

Johannes

你可以拥有它:

快速,准确,便宜。

选择两个。

#define ASSERT(xy,MSG) do \
{ \
if(!(xy))) \
{ \
fprintf(stderr,"Errormessage: %s",
MSG);\
} \
}while(0)

Use: ASSERT(a==b,"My Message to you")

you might insert a call to assert after the fprintf. or simply
exit(EXIT_FAILURE)...

--
Johannes
You can have it:
Quick, Accurate, Inexpensive.
Pick two.


priyanka schrieb:
priyanka schrieb:

有谁能告诉我assert()函数是如何工作的?我需要开发
Can anyone show me how the assert() function works ? I need to develop



断言是一个宏。

assert is a macro.


我自己的assert()函数而不是使用在

assert.h文件中定义的那个。如果有人能在assert.h头文件中向我展示assert()函数的实际源代码,那就太棒了。


我需要写一下断言当

表达式错误时打印出错误信息的函数。例如,


int a = 2;

int b = 3;

ASSERT(a == b);
my own assert() function instead of using the one defined in the
assert.h file. It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.

I need to write the assert function that prints out error message when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);



这个版本的ASSERT可以通过定义NDEBUG来关闭,就像

原始断言宏一样;在

中使用的技巧呼叫 ASSERT使断言打印出来

自定义错误消息适用于许多实现''

断言。


,----

#include< stdio.h>


#define stringize(s)#s

#定义XSTR(s)stringize(s)

#if!定义NDEBUG

void abort(void);

#如果定义了__STDC_VERSION__& &安培; __STDC_VERSION__> = 199901L

#define ASSERT(a)\

do {\

if(0 ==(a)) {\

fprintf(stderr,\

"断言失败:%s," \

"%s( ),%d at \''%s \''\ n",\

__FILE __,\

__func__,\

__LINE __,\

XSTR(a)); \

abort(); \

} \

}而(0)

#else

#define ASSERT(a) \

做{\

if(0 ==(a)){\

fprintf(stderr,\

断言失败:%s,\

"%d at \''%s \''\ n",\

__FILE __,\

__LINE __,\

XSTR(a)); \

abort(); \

} \

}而(0)

#endif

#else

#define ASSERT(a)(无效)0

#endif


int main(无效)

{

ASSERT(1 == 0

&&我是教皇);

返回0;

}

` ----


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。

This version of ASSERT can be switched off just like the
original assert macro via defining NDEBUG; the trick used in
the "Call" of ASSERT to make the assertion print out a
customized error message works for many implementations''
assert as well.

,----
#include <stdio.h>

#define stringize(s) #s
#define XSTR(s) stringize(s)
#if !defined NDEBUG
void abort (void);
# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
# define ASSERT(a) \
do { \
if (0 == (a)) { \
fprintf(stderr, \
"Assertion failed: %s, " \
"%s(), %d at \''%s\''\n", \
__FILE__, \
__func__, \
__LINE__, \
XSTR(a)); \
abort(); \
} \
} while (0)
# else
# define ASSERT(a) \
do { \
if (0 == (a)) { \
fprintf(stderr, \
"Assertion failed: %s, " \
"%d at \''%s\''\n", \
__FILE__, \
__LINE__, \
XSTR(a)); \
abort(); \
} \
} while (0)
# endif
#else
# define ASSERT(a) (void)0
#endif

int main (void)
{
ASSERT(1 == 0
&& "I am the pope");
return 0;
}
`----

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


" priyanka" < pr ********** @ gmail.comwrites:
"priyanka" <pr**********@gmail.comwrites:

有人能告诉我assert()函数是如何工作的吗?我需要开发

我自己的assert()函数,而不是使用

assert.h文件中定义的函数。
Can anyone show me how the assert() function works ? I need to develop
my own assert() function instead of using the one defined in the
assert.h file.



为什么?预定义的断言宏有什么问题吗?

Why? Is there something wrong with the predefined assert macro?


如果有人能告诉我断言的实际

源代码会很棒( )assert.h头文件中的函数。
It would be great if anyone could show me the actual
source code of assert() function in assert.h header file.



如果你有一个C实现,你几乎肯定有一个assert.h

文件,你可以自己看看。

If you have a C implementation, you almost certainly have an assert.h
file that you can look at yourself.


我需要编写断言函数,当

表达式错误时打印出错误信息。例如,


int a = 2;

int b = 3;

ASSERT(a == b);
I need to write the assert function that prints out error message when
the expression is wrong. For eg.,

int a = 2;
int b=3;
ASSERT (a == b);



这就是预定义断言的作用。你为什么要重新发明

轮?


(我能想到的唯一理由就是作为家庭作业

如果这就是这个,那就自己动手吧。)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< ; *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

That''s what the predefined assert does. Why are you reinventing the
wheel?

(The only reason I can think of to do this is as a homework
assignment. If that''s what this is, do it yourself.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于写我自己的断言()的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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