if是说,这个case和else的case都有可能,而且都我能处理的
assert是说,这是个我不能处理的情况;换句话说,要想用我这个函数,必须的保证assert的东西为真,不然我不能处理
技巧1:记住ASSERT的定义
对许多开发人员来说,断言是一个令人困惑的话题,因为它们的许多使用方式与其设计初衷背道而驰.我见到的最清晰的断言定义是这样的:
"断言是在程序某个特定点的一个布尔表达式,除非程序中有缺陷(Bug),否则它的值将为真."
想要理解上述断言定义的开发人员应该留意下面三个要点:
-断言会评估一个表达式是真还是假
-断言是在代码中的某个点对系统状态的一种假设
-断言会验证系统假设,如果不为真,就表明代码中有一个缺陷
断言非常适合契约式设计环境,在这种环境中,开发人员非常清晰地定义了某个函数的先决条件.断言可以用来检查该函数的输入是否满足先决条件.就拿图1所示的代码片段为例:
图1:函数的先决条件
在State不小于最大值的事件中,断言表达式将被评估为假,程序于是将停止执行.停止程序执行可以让开发人员很容易马上看到哪里的代码出错,而不是过段时间以后才知道.
开发人员在查看上述代码后可能会感到这些检查毫无意义.刚刚才设置好的SystemState怎么就会出现大于SYSTEM_STATE_MAX的值呢?答案是这确实不应该出现,然而有时候会莫名其妙地发生改变,也许是通过中断或并行线程,此时断言可以立即标志出这个缺陷.
读者可以清楚地看到,试图打开文件的结果与文件系统的状态和用户数据有关,而与代码中的缺陷一点关系也没有.开发人员应该编写错误处理程序,而不是用断言,以便在文件不存在时,错误处理程序可以用一些默认可用数据来创建它,以便后续代码继续操作.
开发ASSERT宏的原始意图是在开发过程中启用它,在后面生产时要禁用.可以用NDEBUG宏激活和禁用ASSERT.正确实施的断言在被禁用后应该对嵌入式系统基本没有影响.
问题是,如果测试是在断言启用的情况下进行的(为了捕捉任何缺陷,应该这样做),那么现在禁用断言将导致交付的产品与测试的产品处于不同的状态.断言确实会占用一些代码空间,但更重要的是,它们需要占用少量的时钟周期来评估它们的布尔表达式.禁用ASSERT可能对具有有限资源的裸机系统的执行时序产生很大影响,从而导致在生产系统中产生新的缺陷.开发团队需要判断是否值得冒关闭断言的.风险.
一种替代方案是保留断言在激活状态,而将它们的输出重定向到一个系统日志.这样可以确保任何挥之不去的缺陷很容易被识别,而且能避免中止系统的运行,而中止系统可不是明智之举.
ASSERT的默认实现允许开发人员包含一段可执行代码作为布尔表达式的一部分.举例来说,一个状态变量可以被实现为表达式的一部分并传递给ASSERT.但如果传递给ASSERT的表达式有副作用,也就是说,它会改变嵌入式系统的状态,那么禁用断言将改变系统的行为.开发人员应该确保他们的表达式没有副作用,否则他们需要冒险在系统中增加只针对产品代码唤醒的休眠时间缺陷.
断言可以生成极好的注释!编写出色的表达式可以确切地告诉开发人员在代码的某个给定点应该预料发生什么事情.开发人员应该做好他们断言的架构,帮助人们更清楚地理解系统中发生的事情,进而帮助减少缺陷.
小结
断言是一种出色的工具,但有太多的嵌入式软件开发人员忽视了这一工具.本文讨论的八个技巧只是如何正确使用断言的冰山一角.此时此刻呢读者就可以在测试平台中建立和开始使用断言,并研究它们在实际的嵌入式系统中是如何工作的.
是程序调试很重要的手段,
ASSERT(
f
)
在Debug模式下,每次运行到这里后会计算括号中的表达式,如果表达式为0,则中断执行,弹出一个警告框,用户可选择"继续","重试","忽略"
在Release模式下,这句语句不会被编译进代码.
ASSERT一般用于程序内部确认参数的正确性,即调用内部函数的时候,要由调用者保证参数的正确,而被调用函数内部,就可以通过ASSERT来检查参数是否满足要求.
函数名: abort
功 能: 异常终止一个进程
用 法: void abort(void);
程序例:
#include stdio.h
#include stdlib.h
int main(void)
{
printf("Calling abort()\n");
abort();
return 0; /* This is never reached */
}
函数名: abs
功 能: 求整数的绝对值
用 法: int abs(int i);
#include math.h
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
函数名: absread, abswirte
功 能: 绝对磁盘扇区读、写数据
用 法: int absread(int drive, int nsects, int sectno, void *buffer);
int abswrite(int drive, int nsects, in tsectno, void *buffer);
/* absread example */
#include conio.h
#include process.h
#include dos.h
int i, strt, ch_out, sector;
printf("Insert a diskette into drive A and press any key\n");
getch();
sector = 0;
if (absread(0, 1, sector, buf) != 0)
perror("Disk problem");
exit(1);
printf("Read OK\n");
ch_out = buf[strt+i];
putchar(ch_out);
printf("\n");
return(0);
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
#include io.h
int file_exists(char *filename);
printf("Does NOTEXIST.FIL exist: %s\n",
file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
int file_exists(char *filename)
return (access(filename, 0) == 0);
函数名: acos
功 能: 反余弦函数
用 法: double acos(double x);
double result;
result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
函数名: allocmem
功 能: 分配DOS存储段
用 法: int allocmem(unsigned size, unsigned *seg);
#include alloc.h
unsigned int size, segp;
int stat;
stat = allocmem(size, segp);
if (stat == -1)
printf("Allocated memory at segment: %x\n", segp);
else
printf("Failed: maximum number of paragraphs available is %u\n",
stat);
函数名: arc
功 能: 画一弧线
用 法: void far arc(int x, int y, int stangle, int endangle, int radius);
#include graphics.h
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;
/* initialize graphics and local variables */
initgraph(gdriver, gmode, "");
/* read result of initialization */
errorcode = graphresult(); /* an error occurred */
if (errorcode != grOk)
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
exit(1); /* terminate with an error code */
setcolor(getmaxcolor());
/* draw arc */
arc(midx, midy, stangle, endangle, radius);
/* clean up */
closegraph();
函数名: asctime
功 能: 转换日期和时间为ASCII码
用 法: char *asctime(const struct tm *tblock);
#include string.h
#include time.h
struct tm t;
/* sample loading of tm structure */
t.tm_sec = 1; /* Seconds */
t.tm_mon = 11; /* Month */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
/* converts structure to null terminated
string */
strcpy(str, asctime(t));
printf("%s\n", str);
函数名: asin
功 能: 反正弦函数
用 法: double asin(double x);
result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
函数名: assert
功 能: 测试一个条件并可能使程序终止
用 法: void assert(int test);
#include assert.h
struct ITEM {
int key;
int value;
};
/* add item to list, make sure list is not null */
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
/* add item to list */
additem(NULL);
函数名: atan
功 能: 反正切函数
用 法: double atan(double x);
result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
功 能: 计算Y/X的反正切值
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
函数名: atexit
功 能: 注册终止函数
用 法: int atexit(atexit_t func);
void exit_fn1(void)
printf("Exit function #1 called\n");
/* post exit function #1 */
atexit(exit_fn1);
函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
float f;
f = atof(str);
printf("string = %s float = %f\n", str, f);
函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
int n;
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
函数名: atol
用 法: long atol(const char *nptr);
long l;
l = atol(lstr);
printf("string = %s integer = %ld\n", str, l);
assert在C语言中称为断言,用来提示一些可能存在的错误.
编写代码时,做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式.断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真.可以在任何时候启用和禁用断言验证,所以呢可以在测试时启用断言,而在部署时禁用断言.同样,程序投入运行后,最终用户在遇到问题时可以重新起用断言.
assert_param(IS_GPIO_MODE(GPIO_InitStruct-GPIO_Mode));
意思是:IS_GPIO_MODE(GPIO_InitStruct-GPIO_Mode)这个判断条件必须为真,否则程序就会进入死循环.
一般assert用来判断必须为真的一些条件,防止程序出现意外错误.
例如:
开汽车
以上就是土嘎嘎小编为大家整理的c语言的assert函数相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!