C multi line macro: do/while(0) vs scope block
http://docs.google.com/View?docid=ajbgz6fp3pjh_2dwwwwt#_38239340844832237
It is not about optimization.
The whole idea of using 'do/while' version
is to make a macro which will
expand into a regular statement, not into a
compound statement. This is
done in order to make the use of function-style
macros uniform with the
use of ordinary functions in all
contexts.
Consider the following code sketch
if
(<condition>)
foo(a);
else
bar(a);
where 'foo' and 'bar'
are ordinary functions. Now imagine that you'd
like to replace function 'foo'
with a macro of the above nature
if
(<condition>)
CALL_FUNCS(a);
else
bar(a);
Now, if your
macro is defined in accordance with the second approach
(just '{' and '}')
the code will no longer compile, because the 'true'
branch of 'i' is now
represented by a compound statement. And when you
put a ';' after this
compound statement, you finished the whole 'if'
statement, thus orphaning the
'else' branch (hence the compilation error).
One way to correct this
problem is to remember not to put ';' after
macro "invocations"
if
(<condition>)
CALL_FUNCS(a)
else
bar(a);
This will compile
and work as expected, but this is not uniform. The
more elegant solution is
to make sure that macro expand into a regular
statement, not into a compound
one. One way to achieve that is to define
the macro as follows
#define
CALL_FUNCS(x) \
do { \
func1(x); \
func2(x); \
func3(x); \
}
while (0)
Now this code
if
(<condition>)
CALL_FUNCS(a);
else
bar(a);
will compile
without any problems.
However, note the small but important difference
between my definition
of 'CALL_FUNCS' and the first version in your message.
I didn't put a
';' after '} while (0)'. Putting a ';' at the end of that
definition
would immediately defeat the entire point of using 'do/while' and
make
that macro pretty much equivalent to the compound-statement version.
相关文档:
在Perl的各种文件测试运算符中,最有用的运算符之一就是 -C 了。
它返回的是文件的ctime到程序启动的那一时刻经过的天数。
如果不是整数天,返回值就带有小数。
这个运算符经常用来检测文件是否过期,比如判断文件距离上次修改是否超过了3天,
如果超过就将其删除。但是,你是否真正理解 -C 的工作原理呢?
何谓ctime
......
from: 《自己动手写操作系统》
1. 中断向量表 查看 linux/init/main.c in http://lxr.linux.no/#linux+v2.6.32/init/main.c
2.
; [root@XXX XXX]# nasm -f elf foo.asm -o foo.o
; [root@XXX XXX]# gcc -c bar.c -o bar.o
; [root@XXX XXX]# ld -s foo.o bar.o -o foobar
; [root@XXX XXX]# ./foobar
; the 2nd on ......
通过前面两篇,相信大家已经基本了解了我们的研究思路。既然是研究,那么必须的研究工具就得熟练掌握了。这里我所指的研究工具就是VC,我所使用的VC版本是2005。本文也将根据VC2005进行探讨。可能很多朋友或者初学者还是使用的VC6.0。在这里本人不推荐使用VC6.0。原因很简单,VC6.0已经过时了,后面的版本比VC6.0更强大方便 ......
http://os.51cto.com 2008-03-21 11:15 佚名 赛迪网
摘要:学会使用vim/emacs,vim/emacs是linux下最常用的源码编辑器,不光要学会用它们编辑源码,还要学会用它们进行查找、定位、替换等。新手的话推荐使用vim,这也是我目前使用的文本编辑器。
标签:Linux C语言 编程
Or ......
本文旨在说明如何利用Flash和C语言制作BS模式下的实时数据动态曲线图,现在流行的实时数据曲线图,大都是采用了CS模式的开发语言,BS模式的虽有一些实例,比如google上的股市曲线图,但其实现的方式和流程在网上很少能见到。
其运行时的界面如下,数据实时更新,曲线图从左往右动态移动:
下面先讲在实现过程中的工作环境 ......