c语言的assert函数,c++中assert的用法-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

c语言的assert函数,c++中assert的用法

c语言 自写字符串函数处理为什么用assert断言,而不是用if来判断。

if是说,这个case和else的case都有可能,而且都我能处理的

目前创新互联已为上1000家的企业提供了网站建设、域名、虚拟主机网站托管、企业网站设计、保德网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

assert是说,这是个我不能处理的情况;换句话说,要想用我这个函数,必须的保证assert的东西为真,不然我不能处理

C语言中用ASSERT调试的八大技巧

技巧1:记住ASSERT的定义

对许多开发人员来说,断言是一个令人困惑的话题,因为它们的许多使用方式与其设计初衷背道而驰。我见到的最清晰的断言定义是这样的:

“断言是在程序某个特定点的一个布尔表达式,除非程序中有缺陷(Bug),否则它的值将为真。”

想要理解上述断言定义的开发人员应该留意下面三个要点:

·断言会评估一个表达式是真还是假

·断言是在代码中的某个点对系统状态的一种假设

·断言会验证系统假设,如果不为真,就表明代码中有一个缺陷

技巧2:使用ASSERT验证函数的先决条件

断言非常适合契约式设计环境,在这种环境中,开发人员非常清晰地定义了某个函数的先决条件。断言可以用来检查该函数的输入是否满足先决条件。就拿图1所示的代码片段为例:

图1:函数的先决条件

函数的STate输入应该在定义的系统状态范围内。如果State不是有效的状态值,那么它就不是错误,而是缺陷!断言可以用来验证State是有效的假设,如图2所示:

图2:对函数先决条件应用断言

在State不小于最大值的事件中,断言表达式将被评估为假,程序于是将停止执行。停止程序执行可以让开发人员很容易马上看到哪里的代码出错,而不是过段时间以后才知道。

技巧3:使用ASSERT验证函数的后置条件

断言也能用来验证契约式设计环境中对某个函数输出的假设。例如,如果前面定义的System_StateSet函数返回SystemState变量,开发人员可以预计它也在期望的范围之内。断言可以用来对缺陷进行监视,如图3所示。

图3:对函数后置条件应用断言

开发人员在查看上述代码后可能会感到这些检查毫无意义。刚刚才设置好的SystemState怎么就会出现大于SYSTEM_STATE_MAX的值呢?答案是这确实不应该出现,然而有时候会莫名其妙地发生改变,也许是通过中断或并行线程,此时断言可以立即标志出这个缺陷。

技巧4:不要把ASSERT用于错误处理

在记住断言定义之后,开发人员应该切记:断言是用于检测缺陷的,不能用于错误处理。错误处理是设计用于响应错误的用户输入和意外的事件顺序的软件。错误在系统中预料是会发生的,但仅仅是因为有无效的输入而并不意味着代码中有缺陷。错误处理应该与缺陷寻找分开来。错误使用断言的一个典型例子是,在试图打开一个文件用于读取时去检查文件的指针,如图4所示。

图4:ASSERT的不当使用

读者可以清楚地看到,试图打开文件的结果与文件系统的状态和用户数据有关,而与代码中的缺陷一点关系也没有。开发人员应该编写错误处理程序,而不是用断言,以便在文件不存在时,错误处理程序可以用一些默认可用数据来创建它,以便后续代码继续操作。

技巧5:ASSERT仅对开发有意义,不能用于生产

开发ASSERT宏的原始意图是在开发过程中启用它,在后面生产时要禁用。可以用NDEBUG宏激活和禁用ASSERT。正确实施的断言在被禁用后应该对嵌入式系统基本没有影响。

问题是,如果测试是在断言启用的情况下进行的(为了捕捉任何缺陷,应该这样做),那么现在禁用断言将导致交付的产品与测试的产品处于不同的状态。断言确实会占用一些代码空间,但更重要的是,它们需要占用少量的时钟周期来评估它们的布尔表达式。禁用ASSERT可能对具有有限资源的裸机系统的执行时序产生很大影响,从而导致在生产系统中产生新的缺陷。开发团队需要判断是否值得冒关闭断言的.风险。

一种替代方案是保留断言在激活状态,而将它们的输出重定向到一个系统日志。这样可以确保任何挥之不去的缺陷很容易被识别,而且能避免中止系统的运行,而中止系统可不是明智之举。

技巧6:不允许断言有副作用

ASSERT的默认实现允许开发人员包含一段可执行代码作为布尔表达式的一部分。举例来说,一个状态变量可以被实现为表达式的一部分并传递给ASSERT。但如果传递给ASSERT的表达式有副作用,也就是说,它会改变嵌入式系统的状态,那么禁用断言将改变系统的行为。开发人员应该确保他们的表达式没有副作用,否则他们需要冒险在系统中增加只针对产品代码唤醒的休眠时间缺陷。

技巧7:断言应该占代码的1%至3%

每个开发人员对于代码库(Code Base)中应该有多少个断言都有自己的主见。大家一致同意的一个数字是,代码库中的断言占比应该大于0。断言为开发人员提供了一种在代码库中发生缺陷的时刻发现它的好方法。调试是在开发嵌入式系统中最浪费时间并令人沮丧的事情之一。不管开发人员认可的占比是1%、3%还是5%,使用断言肯定对你有利,并会使开发嵌入式软件变得多少有些趣味。

技巧8:将断言用作可执行代码注释

断言可以生成极好的注释!编写出色的表达式可以确切地告诉开发人员在代码的某个给定点应该预料发生什么事情。开发人员应该做好他们断言的架构,帮助人们更清楚地理解系统中发生的事情,进而帮助减少缺陷。

小结

断言是一种出色的工具,但有太多的嵌入式软件开发人员忽视了这一工具。本文讨论的八个技巧只是如何正确使用断言的冰山一角。接下来读者就可以在测试平台中建立和开始使用断言,并研究它们在实际的嵌入式系统中是如何工作的。

ASSERT在C语言中有什么作用?

是程序调试很重要的手段,

ASSERT(

f

)

在Debug模式下,每次运行到这里后会计算括号中的表达式,如果表达式为0,则中断执行,弹出一个警告框,用户可选择“继续”,“重试”,“忽略”

在Release模式下,这句语句不会被编译进代码。

ASSERT一般用于程序内部确认参数的正确性,即调用内部函数的时候,要由调用者保证参数的正确,而被调用函数内部,就可以通过ASSERT来检查参数是否满足要求。

求C语言函数大全

函数名: 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 stdio.h

#include math.h

int main(void)

{

int number = -1234;

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 stdio.h

#include conio.h

#include process.h

#include dos.h

int main(void)

{

int i, strt, ch_out, sector;

char buf[512];

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");

strt = 3;

for (i=0; i80; i++)

{

ch_out = buf[strt+i];

putchar(ch_out);

}

printf("\n");

return(0);

}

函数名: access

功 能: 确定文件的访问权限

用 法: int access(const char *filename, int amode);

程序例:

#include stdio.h

#include io.h

int file_exists(char *filename);

int main(void)

{

printf("Does NOTEXIST.FIL exist: %s\n",

file_exists("NOTEXISTS.FIL") ? "YES" : "NO");

return 0;

}

int file_exists(char *filename)

{

return (access(filename, 0) == 0);

}

函数名: acos

功 能: 反余弦函数

用 法: double acos(double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 0.5;

result = acos(x);

printf("The arc cosine of %lf is %lf\n", x, result);

return 0;

}

函数名: allocmem

功 能: 分配DOS存储段

用 法: int allocmem(unsigned size, unsigned *seg);

程序例:

#include dos.h

#include alloc.h

#include stdio.h

int main(void)

{

unsigned int size, segp;

int stat;

size = 64; /* (64 x 16) = 1024 bytes */

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);

return 0;

}

函数名: arc

功 能: 画一弧线

用 法: void far arc(int x, int y, int stangle, int endangle, int radius);

程序例:

#include graphics.h

#include stdlib.h

#include stdio.h

#include conio.h

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

int midx, midy;

int stangle = 45, endangle = 135;

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:");

getch();

exit(1); /* terminate with an error code */

}

midx = getmaxx() / 2;

midy = getmaxy() / 2;

setcolor(getmaxcolor());

/* draw arc */

arc(midx, midy, stangle, endangle, radius);

/* clean up */

getch();

closegraph();

return 0;

}

函数名: asctime

功 能: 转换日期和时间为ASCII码

用 法: char *asctime(const struct tm *tblock);

程序例:

#include stdio.h

#include string.h

#include time.h

int main(void)

{

struct tm t;

char str[80];

/* sample loading of tm structure */

t.tm_sec = 1; /* Seconds */

t.tm_min = 30; /* Minutes */

t.tm_hour = 9; /* Hour */

t.tm_mday = 22; /* Day of the Month */

t.tm_mon = 11; /* Month */

t.tm_year = 56; /* Year - does not include century */

t.tm_wday = 4; /* Day of the week */

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);

return 0;

}

函数名: asin

功 能: 反正弦函数

用 法: double asin(double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 0.5;

result = asin(x);

printf("The arc sin of %lf is %lf\n", x, result);

return(0);

}

函数名: assert

功 能: 测试一个条件并可能使程序终止

用 法: void assert(int test);

程序例:

#include assert.h

#include stdio.h

#include stdlib.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 */

}

int main(void)

{

additem(NULL);

return 0;

}

函数名: atan

功 能: 反正切函数

用 法: double atan(double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 0.5;

result = atan(x);

printf("The arc tangent of %lf is %lf\n", x, result);

return(0);

}

函数名: atan2

功 能: 计算Y/X的反正切值

用 法: double atan2(double y, double x);

程序例:

#include stdio.h

#include math.h

int main(void)

{

double result;

double x = 90.0, y = 45.0;

result = atan2(y, x);

printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);

return 0;

}

函数名: atexit

功 能: 注册终止函数

用 法: int atexit(atexit_t func);

程序例:

#include stdio.h

#include stdlib.h

void exit_fn1(void)

{

printf("Exit function #1 called\n");

}

void exit_fn2(void)

{

printf("Exit function #2 called\n");

}

int main(void)

{

/* post exit function #1 */

atexit(exit_fn1);

/* post exit function #2 */

atexit(exit_fn2);

return 0;

}

函数名: atof

功 能: 把字符串转换成浮点数

用 法: double atof(const char *nptr);

程序例:

#include stdlib.h

#include stdio.h

int main(void)

{

float f;

char *str = "12345.67";

f = atof(str);

printf("string = %s float = %f\n", str, f);

return 0;

}

函数名: atoi

功 能: 把字符串转换成长整型数

用 法: int atoi(const char *nptr);

程序例:

#include stdlib.h

#include stdio.h

int main(void)

{

int n;

char *str = "12345.67";

n = atoi(str);

printf("string = %s integer = %d\n", str, n);

return 0;

}

函数名: atol

功 能: 把字符串转换成长整型数

用 法: long atol(const char *nptr);

程序例:

#include stdlib.h

#include stdio.h

int main(void)

{

long l;

char *str = "98765432";

l = atol(lstr);

printf("string = %s integer = %ld\n", str, l);

return(0);

}

C语言 assert干什么用的

assert在C语言中称为断言,用来提示一些可能存在的错误。

编写代码时,做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式。断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真。可以在任何时候启用和禁用断言验证,因此可以在测试时启用断言,而在部署时禁用断言。同样,程序投入运行后,最终用户在遇到问题时可以重新起用断言。

关于C语言中assert等语法在嵌入式应用当中的含义

assert_param(IS_GPIO_MODE(GPIO_InitStruct-GPIO_Mode));

意思是:IS_GPIO_MODE(GPIO_InitStruct-GPIO_Mode)这个判断条件必须为真,否则程序就会进入死循环。

一般assert用来判断必须为真的一些条件,防止程序出现意外错误。

例如:

assert(汽车有4个轮子)//这个是必须成立的条件

开汽车


名称栏目:c语言的assert函数,c++中assert的用法
标题链接:http://kswsj.cn/article/hciodh.html

其他资讯