If you are a C programmer, you must be familiar with macros. They are powerful and can help you ease your work if used correctly. However, if you don't define macros carefully, they may bite you and drive you crazy. In many C programs, you may see a special macro definition which may seem not so straightforward. Here is one example:
1

2

#define __set_task_state(tsk, state_value) \

do{ (tsk)->state = (state_value); } while(0)

There are many this kind of macros which uses do{...}while(0) in Linux kernels and other popular C libraries. What's the use of this macro? Robert Lovefrom Google(previously worked on Linux kernel development) gives us the answer.

do{...}while(0) is the only construct in C that lets you define macros that always work the same way, so that a semicolon after your macro always has the same effect, regardless of how the macro is used (with particularly emphasis on the issue of nesting the macro in an if without curly-brackets). For example:

1

#define foo(x) bar(x); baz(x)

Later you may call:

1

foo(wolf);

This will be expanded to:

1

bar(wolf); baz(wolf);

This is the expected output. Next let's see if we have:

1

2

if(!feral)

foo(wolf);

The expansion may not be what you expect. The expansion may be:

1

2

3

if(!feral)

bar(wolf);

baz(wolf);

It isn't possible to write multistatement macros that do the right thing in all situations. You can't make macros behave like functions—without do/while(0).

If we redefine the macro with do{...}while(0), we will see:

1

#define foo(x) do { bar(x); baz(x); } while (0)

Now, this statement is functionally equivalent to the former. The do ensures the logic inside the curly-brackets executes, the while(0) ensures that happens but once. Same as without the loop.For the above if statement, it will be :

1

2

if(!feral)

do{ bar(wolf); baz(wolf); } while(0);

Semantically, it's the same as:

1

2

3

4

if(!feral) {

bar(wolf);

baz(wolf);

}

You might rejoin, why not justwrap the macro in curly-brackets? Why also have the do/while(0)logic? For example, we define the macro with curly bracket:

1

#define foo(x) { bar(x); baz(x); }

This is fine for the above if statement, but if we have below statement:

1

2

3

4

if(!feral)

foo(wolf);

else

bin(wolf);

The expanded code will be :

1

2

3

4

5

6

if(!feral) {

bar(wolf);

baz(wolf);

};

else

bin(wolf);

This is a syntax error.

In conclusion, macros in Linux and other codebases wrap their logic in do/while(0) because it ensures the macro always behaves the same, regardless of how semicolons and curly-brackets are used in the invoking code.

do {...} while (0) in macros的更多相关文章

  1. 用m4 macros创建文本文件

    用m4 macros创建文本文件   原文链接: http://ldp.linux.no/linuxfocus/ChineseGB/September1999/article111.html 补充阅读 ...

  2. ATL and MFC String Conversion Macros

    ATL 7.0介绍了一些新的转换类和宏,为现有的宏提供了重要的改进.新的字符串转换类和名称宏的形式是:C 源类型 2[C] 目标类型[EX]其中:•源类型和目标类型描述如下表.• [C]是目标类型必须 ...

  3. 7、zabbix使用进阶(03)

    节知识点: zabbix自动发现 web监控 zabbix自动发现   官网:https://www.zabbix.com/documentation/4.0/zh/manual/discovery/ ...

  4. Android Mokoid Open Source Project hacking

    /***************************************************************************** * Android Mokoid Open ...

  5. CV_Assert

    转:http://blog.csdn.net/ding977921830/article/details/46376847 Checks a condition at runtime and thro ...

  6. 基于Arduino的音乐动感节奏灯

    1.音乐动感节奏灯是个什么东西? 前段时间听音乐觉得无聊,便想着音乐光听也没意思啊,能不能 “看见” 音乐呢?于是谷歌了一番,发现还真有人做了将音乐可视化的东西,那就是音乐节奏灯.说的简单点就是LED ...

  7. 恶意代码检测工具 -- Mathematics Malware Detected Tools

    Mathematics Malware Detected Tools 重要:由于缺少测试数据,部分结论可能不正确.更多更准确的结论,还需要进行大量实验. 概述 mmdt(Mathematics Mal ...

  8. [Linux][C][gcc][tips] 在头文件中定义变量引发的讨论

    概述 本人的原创文章,最先发表在github-Dramalife-note中.转载请注明出处. Define variable(s) in header file referenced by mult ...

  9. ZAM 3D 制作简单的3D字幕 流程(二)

    原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...

随机推荐

  1. 导入CSV文件之后出现换行符问题

    在用sqlldr导入数据之后,出现数据无法匹配的情况 在用plsql点击的时候,发现出现换行符的情况,从而使用下面的方法进行匹配 select q.comments from q where repl ...

  2. 在asp.net mvc中使用PartialView返回部分HTML段

    问题链接: MVC如何实现异步调用输出HTML页面 该问题是个常见的 case, 故写篇文章用于提示新人. 在asp.net mvc中返回View时使用的是ViewResult,它继承自ViewRes ...

  3. Blogilo:Ubuntu下“wlw”

    原文首发:http://www.ido321.com/1274.html 这一篇博客是在Ubuntu的博客客户端blogilo下发布的,感觉有点高大上,特此发布一篇博文记录一下.      博客发布在 ...

  4. 《APUE》中的函数整理

    第1章 unix基础知识 1. char *strerror(int errnum) 该函数将errnum(就是errno值)映射为一个出错信息字符串,返回该字符串指针.声明在string.h文件中. ...

  5. Java Client for Google Cloud Storage

    关于Google Cloud Storage Google Cloud Storage有益于大文件的存储与服务(serve).此外,Cloud Storage提供了对访问控制列表(ACLs)的使用,提 ...

  6. Linux服务器集群系统(一)(转)

    add by zhj:虽然是2002年的文章,但读来还是收益良多.在 章文嵩:谈LVS及阿里开源背后的精彩故事 中LVS发起人及主要贡献者谈了LVS的开发过程及阿里开源的一些故事 原文:http:// ...

  7. Red5源代码分析 - 关键类及其初始化过程

    原文地址:http://semi-sleep.javaeye.com/blog/348768 Red5如何响应rmpt的请求,中间涉及哪些关键类? 响应请求的流程如下: 1.Red5在启动时会调用RT ...

  8. 随便看看My97DatePicker源码J方法

    如果有一个路径是写错的并且这个路径是写在前面,那么相关的css文件你就别想引进来了 <script language="javascript" type="text ...

  9. mysql一些写常用命令

    参见pcttcnc2007博客腾飞 1.mysql的status信息命令: mysql> show global status; 2.可以列出mysql服务器运行各种状态值,另外,查询mysql ...

  10. hdu 2824 The Euler function

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...