What is the purpose of using do {...} while (0) in macros?

USB gtco linux驱动代码 【代码】USB gtco linux驱动代码。 阅读详情
Thedo/while(0) pattern seen in many if not most macros in the Linux kernel and elsewhere has a specific purpose: It is the only construct in C that lets you define macros thatalways 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).

Let's consider an example:

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


Say you used it like this:

foo(wolf);


This expands to:

bar(wolf); baz(wolf);


So far, so good. That does what you intended. But let's consider usage like this:


if (!feral)
    foo(wolf);


This expands to:

if (!feral)
    bar(wolf); baz(wolf);


Which is, if sanely formatted:

if (!feral)
    bar(wolf);
baz(wolf);


That likely isn't what you intended. As shown, it isn't possible to write multistatement macros that do the right thing in all situations. You can't make macros behave like functions—withoutdo/while(0).

Let's revisit our original macro, wrapped in do/while(0):

#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, thewhile(0) ensures that happens but once. Same as without the loop. So what's different? Let's look at using the macro. Again:

if (!feral)
    foo(wolf);


Which now becomes:

if (!feral)
    do { bar(wolf); baz(wolf); } while (0);


Which is the same as:

if (!feral) {
    bar(wolf);
    baz(wolf);
}


You might rejoin, why not just wrap the macro in curly-brackets? Why also have thedo/while(0) logic?

This is why. Consider:

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


And:

if (!feral)
    foo(wolf);
else
    bin(wolf);


This becomes:

if (!feral) {
    bar(wolf);
    baz(wolf);
};
else
    bin(wolf);


Note the stray semicolon, which makes the else a dangling else. If you want people to be able to use your macro like a function and put semicolons where they belong, then this doesn't work.
USB GTCO linux 驱动代码 【代码】USB GTCO linux 驱动代码。 阅读详情

相关推荐

zabbix 监控EMC存储

经过搜索,找到了脚本地址,该项目应该是模板原作者创建的,该项目也有zabbix模板,和上面的应该是相同的,我用的是上面的模板。./unity_get_state.py --api_ip='X.X.X.X' --api_port='443' --api_user='zabbix' --api_password='PASSWORD' --storage_name="zabbix web前端配置的主机名" --discovery。修改权限,给zabbix用户执行权限,模板调用时以zabbix用户执行。

wangyfde的专栏 3758

Mac 可设置环境变量的位置、查看和添加PATH环境变量

  Mac 启动加载文件位置(可设置环境变量) -------------------------------------------------------  (1)首先要知道你使用的Mac OS X是什么样的Shell,使用命令 echo $SHELL 如果输出的是:csh或者是tcsh,那么你用的就是C Shell。 如果输出的是:bash,sh,zsh,那么你的用的可能就是...

SiC B2B2C Shop大型B2B2C商城软件产品,使用Java语言开发,高性能高扩展性高安全性分布式。可帮助你快速的建立大型B2B2C电商系统 1798

PG-FP6编程器错误代码解析与高效排障实战指南

在嵌入式系统开发与量产烧录中,编程器是连接开发环境与目标硬件的重要桥梁。其工作原理是通过特定的通信协议(如UART、SWD)与微控制器(MCU)建立连接,完成程序代码的写入、擦除与校验。这项技术的核心价值在于确保固件能够准确、可靠地部署到硬件中,是产品从原型到量产的关键环节。在实际的工程实践中,尤其是在使用瑞萨(Renesas)PG-FP6这类编程器进行调试或批量生产时,工程师常会遇到各种通信失败、编程验证错误或电源异常等问题,这些问题往往以特定的错误代码形式呈现。深入理解这些错误代码背后的硬件连接、电源时

weixin_34387468的博客 581

Xcode中断点的威力

注:本文由破船译自:albertopasca。本文由HoNooD在iosfeed站点上做了推荐。   这里先推荐两篇Xcode相关的文章: Xcode Code Snippets iOS调试 — 基本技巧   本文目录: 1、添加一个特殊的断点     异常断点(Exception breakpoint)     符号断点(Symbolic b

baoyy的专栏 661

手把手教你用OpenCompass评估Qwen1.5-1.8B模型(含数据集配置详解)

本文提供了使用OpenCompass评估Qwen1.5-1.8B-Chat模型的详细实战教程。内容涵盖从创建独立Python环境、下载模型与核心数据集,到编写配置文件、执行评测命令及解读结果的完整流程。重点解析了模型与数据集的配置细节,并分享了性能调优与常见问题排查方法,旨在帮助开发者快速上手系统化的模型评估。

weixin_29228781的博客 138

【新手必看】挖漏洞流程指南,黑客手把手教你挖漏洞月入过万!收藏这篇就够了!(附学习笔记)

【新手必看】挖漏洞流程指南,黑客手把手教你挖漏洞月入过万!收藏这篇就够了!(附学习笔记)【新手必看】挖漏洞流程指南,黑客手把手教你挖漏洞月入过万!收藏这篇就够了!(附学习笔记)

weixin_57514792的博客 1105

CCNA2 - Module 4 Exam Answers (05/08/2008 14:30)

1、A network administrator was called away from the terminal while using Telnet to monitor a remote router. The Telnet session was left connected. If the default Telnet configuration is used...

weixin_33885676的博客 175

【UG/NX二次开发】记录在二次开发中遇到的坑

【UG/NX二次开发】记录在二次开发中遇到的坑

Trust_MJ的博客 2708

C语言中do...while(0)的妙用

在linux内核代码中,经常看到do...while(0)的宏,do...while(0)有很多作用,下面举出几个: 1、避免goto语句: 通常,如果一个函数开始要分配一些资源,然后如果在中途遇到错误则要退出函数,当然,退出前要释放资源,我们的代码可能如下:  1 #defien N 10 2 3 bool Execute() 4 { 5 // ...

aeg84268的博客 901

零倾覆力矩点(ZMP)

从形式上讲,ZMP 指的是空间中相对于一个平面(通常是地平面)抵消接触力矩的点的集合。这个集合实际上是一个轴,而不是一个点,但为了方便起见,让我们按照传统的推导方法来计算地面平面上的 ZMP。这一推导从任何多刚体系统的牛顿-欧拉运动方程开始:其中,为机器人的总质量,为重力矢量,为系统的重心,为加速度,为固定点的角动量变化率。在右侧,表示净接触扭矩,即施加到机器人上的所有接触扭矩之和,坐标取自质心 (COM)。

机器人控制与仿真应用 2372

DPABISurf使用教材

严老师的官方教程:http://rfmri.org/Course DPABISurf版本:dpabi50docker.tar.gz MATLAB版本:2018b 一、数据准备 (1)如果是数据的DICOM格式,整理成以下格式(注意文件夹的命名方式): 如果觉得不是很清楚,可以在官网下载Demo数据(http://rfmri.org/DemoData),参照里面的文件格式进行整理即可。 (2)如果数据是nii格式,整理成以下格式: 这里没有使用官网的demo数据进行学习,而是..

从事脑科学核磁共振方法学研究,在Nature communications等权威期刊发表研究论文,熟练掌握磁共振处理方法和统计学方法,欢迎大家和我交流。 8521

what is the purpose of using translatable in Android strings?

引用: http://stackoverflow.com/questions/3925072/what-is-the-purpose-of-using-translatable-in-android-strings This attribute points out that this attribute will be the same for all locales. Why

Ken的专栏 921

微信小程序|基于小程序+云开发制作一个租房小程序

经济发展的同时伴随着大批人群的流动,租房需求一直是持久不衰的话题,如何租好房,好租房,跟随此文一起制作一个租房小程序,让租房不再困难。

商务合作 / 项目定制 / 学习交流。个人vx:lovely_wml 4930

新安装的Qt5.15.2报错toolchain.prf:76: error: Variable QMAKE_CXX.COMPILER_MACROS is not defined

20260822。

星垂平野阔, 月涌大江流 176

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

protobuf2.5.0 arm_linux

【代码】protobuf2.5.0 arm_linux。

GAORUI_GAO的博客 347

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Ubuntu 20.04 编译 Apache Mesos 1.9.0 过程中遇到的问题

记录 Ubuntu 20.04 编译 Apache Mesos 1.9.0 过程中遇到的问题

wu_weijie的博客 3358

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

VC下'long' followed by 'long' is illegal

long long 类型在VC6.0编译错误 long long int 是GCC(GNU C)的64位表示法。 VC里面只能用__int64 有两种解决办法: 1、只需要把long long 替换成__int64即可, 注意一定要点击下保存按钮否则头文件修改无效还是编译不通过 2、或者是:如下 stdint.h文件重新建立,并输入以下信息

小地盘的诺克萨斯 1万+

CANN/hixl C++代码风格规范

> **Applicable Scope**: The code style specifications apply to all C++ code to improve code readability and consistency. ## Rule List | Rule No. | Rule Name | Category | Severity | |---------|-----

gitblog_01108的博客 337

ffmpeg编码aac过程

把前面的ffmpeg解码aac输出wav的改了下,输出aac。 不太一样的是需要flush encoders,否则aac就输出不了。 另外,rtmpdump的librtmp有问题,会被服务器关掉,需要在服务器上做一些处理。见上一篇分析。 /* The MIT License (MIT) Copyright (c) 2013 winlin Permission is here

OSSRS 4005

FFMPEG视频h264和音频aac混合编码过程

FFMPEG视频h264和音频aac混合编码过程 /* The MIT License (MIT) Copyright (c) 2013 winlin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated docu

OSSRS 5007

FFMPEG推送rtmp流实例

转自:http://blog.csdn.net/win_lin/article/details/12614693 165行代码说明使用ffmpeg的api如何将文件推送到rtmp服务器。 执行: [plain] view plaincopy ./objs/tool /home/winlin/test_22m.flv rtmp://dev:1935/live/live

hjwang1的专栏 7046

FFMPEG编码h264过程

ffmpeg编码h264的细节比aac复杂很多。 /* The MIT License (MIT) Copyright (c) 2013 winlin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated docu

OSSRS 2368

GOTO 语句有害论 | 反对 GOTO 语句的理由

……

u013669912的博客 1878

CANN/ge ArgsUpdater地址刷新示例

- Graph construction entry: `GE` - Operator programming language: `Ascend C` (RTC runtime compilation) - Compilation method: The `.cpp` file compiles the host-side custom op, while kernel source code

gitblog_00836的博客 757

3. 系统编程概念

一个简单的系统编程概念入门

u010933311的博客 982
上一篇: Threads vs. Async
下一篇: offsetof
michaelrun
博客等级 码龄19年 93粉丝 50原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值