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 驱动代码。 阅读详情

相关推荐

Python读取Matlab v7.3数据

文章目录Matlab保存数据Python读取 v7.3 Mat Matlab保存数据 在matlab设置中,常规->MAT文件,选择MATLAB版本7.3或更高版本保存数据。 注意保存数据的文件名为 corre.mat,保存的数据数组为data; 后面python的读取中会用到这两个参数 Python读取 v7.3 Mat 先安装hdf5storage库,再使用以下代码,就可以读取到mat中的数据,注意 import hdf5storage import numpy as np dataDi

weixin_43121914的博客 1082

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

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

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

Windows 24H2升级后eNSP AR_40报错40?VirtualBox虚拟化修复指南

虚拟化技术通过Hypervisor在操作系统与硬件间建立抽象层,使虚拟机能够共享物理资源。其核心原理依赖CPU的VT-x/AMD-V指令集,而Windows 24H2对内核隔离与驱动签名的强化会直接占用或拦截这些虚拟化资源。在工程实践中,VirtualBox作为Type-2型Hypervisor,常因内存完整性开启或版本过旧导致虚拟网卡创建失败,进而引发设备启动中断。典型场景包括网络仿真实验、多系统测试环境搭建等。当eNSP的AR_40路由器依赖VirtualBox引擎时,24H2的安全策略变更便会触发错误

weixin_34375251的博客 217

Xcode中断点的威力

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

baoyy的专栏 661

告别抓包失败!保姆级配置:让Burp+Proxifier稳定抓取任意微信小程序

本文提供了一套完整的Burp+Proxifier配置方案,确保稳定抓取微信小程序数据。从环境准备、工具选型到Burp Suite深度配置和Proxifier精准设置,详细解决了证书安装、进程捕获和乱码解析等核心问题,帮助开发者告别抓包失败,提升调试效率。

weixin_28738165的博客 455

海康视觉算法平台VisionMaster 4.3.0 C# 二次开发01 加载方案并获取结果

第一次使用海康视觉算法平台VisionMaster 4.3.0,项目中要使用这个平台进行视觉处理并获取结果。

StevenChen的博客 7876

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

YOLOv8+BiFPN双向特征金字塔:多尺度目标检测精度提升28%的实战教程

双向特征金字塔网络(BiFPN)是一种强化多尺度特征融合支持“自顶向下+自底向上”的双向特征流动,同时保留不同尺度的细节信息;引入加权融合机制,自动学习不同特征的重要性权重;裁剪冗余节点,在轻量化的同时提升融合效率。

m0_64661770的博客 492

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

使用 Simulink Real-Time 搭建一个完整的实时信号采集与滤波系统模型

本文详细介绍了基于Simulink Real-Time构建实时信号采集与滤波系统的完整流程。主要内容包括:系统配置(模型创建与参数设置)、信号采集模块搭建(模拟信号源或真实硬件连接)、实时滤波器设计(浮点/定点)、输出可视化(Scope/ToWorkspace等)、模型编译与下载到目标机、外部模式在线调试及数据保存方法。系统可实现微秒级实时信号处理,适用于工业控制、航空航天等对时间确定性要求高的场景。文章还提供了优化建议(多速率建模、优先级调度等)和应用场景说明,帮助读者快速掌握实时系统开发的核心技能。

amy_mhd的博客 304

运维简历优化指南,DeepSeek助您运维简历秒变高分!

例如,当掌握了新的运维技术,如 Serverless 架构的运维管理,就将其添加到简历的专业技能部分,并简要描述在相关项目中的应用情况和成果,如 “在 [项目名称] 中,负责 Serverless 架构的运维工作,成功实现了应用的低成本、高弹性运行,降低了运维成本 30%”。上传简历至 DeepSeek,并输入指令:“请根据以下岗位招聘要求,帮我重新解构我的简历,按照【教育背景→实习经历→项目经历→个人评价→技能证书】的模块,输出一份符合下述岗位的简历,提升我的求职竞争力。

大模型大数据攻城狮的专栏 1470

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

黑猫带你学eMMC协议第12篇:生产状态感知(PSA)

1 概述 1.1 什么是PSA Production State Awareness (PSA) 生产状态感知 e•MMC设备可以利用其是否在生产环境中的信息,从而在现场运作时进行不同的操作。 例如,在设备低温焊接(soldering)前,将数据写入emmc,可能导致数据出错,而正在焊接期间写入数据,出错的概率可能会更大。在低温焊接之前,emmc设备可以使用一种特定的内部操作去加载内容,这样就能减少数据出错的概率,而在低温焊接后进行常规的操作就行了。 1.2 psa如何进行工作? PSA存在于emmc5.0

黑猫奥利奥的博客 4262

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

20260822。

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

IDEA的使用指南

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

protobuf2.5.0 arm_linux

【代码】protobuf2.5.0 arm_linux。

GAORUI_GAO的博客 347

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

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

wu_weijie的博客 3358

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

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、付费专栏及课程。

余额充值