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. 无序数组a,求a[i]-a[j]的最大值,且i<j

    一道面试题:对于无序数组a,求a[i]-a[j]的最大值,其中i<j package test; import java.util.Arrays; public class FindMax { ...

  2. 快速上手Android数据库操作

    Android采用关系型数据库SQLite3,它是一个支持SQL轻量级的嵌入式数据库,在嵌入式操作系统上有很广泛的应用,WM采用的也是SQLite3 关于过于.原理方面的东西在这篇文章里不会提到,但是 ...

  3. 《Java数据结构与算法》笔记-CH4-3用栈实现分隔符匹配

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 利 ...

  4. HDU ACM 1496 Equations

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. GCC4.8对new和delete的参数匹配新要求

    一段通信协议的代码,早年在GCC 4.4.VS2013下编译都挺好的,移植到GCC 4.8 ,为C++ 11做准备,在编译的时候发现问题 源代码省略后的版本如下: class Zerg_App_Fra ...

  6. Apache Spark GraphX的体系结构

    1. 整体架构 GraphX 的整体架构(如图 1所示)可以分为三部分. 图 1  GraphX 架构 存储和原语层: Graph 类是图计算的核心类.内部含有 VertexRDD. EdgeRDD ...

  7. POJ 3660 Cow Contest (Floyd)

    http://poj.org/problem?id=3660 题目大意:n头牛两两比赛经过m场比赛后能判断名次的有几头可转 化为路径问题,用Floyd将能够到达的路径标记为1,如果一个点能 够到达剩余 ...

  8. UVaLive 6627 First Date (转换时间)

    题意:给定两个日期,两种不同算闰年的方法,导致日期不同,给定那个慢的,求你求了那个快的. 析:因为算闰年的方法不同,所以我们就要先从1582算到当前时间,算出差了多少天,再加上就好.注意跨月,跨年的情 ...

  9. js两种创建对象方式

    js创建方法的两种方式 <%@ page language="java" contentType="text/html; charset=ISO-8859-1&qu ...

  10. TypeScript学习笔记(七):模块

    JavaScript中的模块 在学习TypeScript的模块之前我们先看看在JavaScript中的模块是如何实现的. 模块的好处 首先我们要了解使用模块的好处都有什么? 模块化.可重用: 封装变量 ...