macro看似只进行简单的文本替换,在预处理阶段它会根据你所定义的macro生成一个hash,稍后再处理的时候,根据遇到的macro名查找hash,得到宏的内容,使用hash存储速度非常快。
下面我们进行一个简单的实验,猜猜程序的运行结果?
macro.c
- #include <assert.h>
- #define WIDTH 40
- #define HEIGHT 80
- #define AREA 2*(WIDTH* HEIGHT)
- #define MAX(a,b) ( ((a) > (b))?(a): (b))
- int main(int argc,char ** argv){
- int i=1;
- int j=2;
- int c;
- c = MAX(i ,j );
- assert(j == 4);
- assert( AREA == 6400);
- }
after post preprocessor:
我们观察一下程序预处理后的信息
gcc -E macro.c > macro.i
- # 1 "macro.c"
- # 1 ""
- # 1 ""
- # 1 "macro.c"
- # 1 "/usr/include/assert.h" 1 3 4
- # 37 "/usr/include/assert.h" 3 4
- # 1 "/usr/include/features.h" 1 3 4
- # 323 "/usr/include/features.h" 3 4
- # 1 "/usr/include/i386-linux-gnu/bits/predefs.h" 1 3 4
- # 324 "/usr/include/features.h" 2 3 4
- # 356 "/usr/include/features.h" 3 4
- # 1 "/usr/include/i386-linux-gnu/sys/cdefs.h" 1 3 4
- # 353 "/usr/include/i386-linux-gnu/sys/cdefs.h" 3 4
- # 1 "/usr/include/i386-linux-gnu/bits/wordsize.h" 1 3 4
- # 354 "/usr/include/i386-linux-gnu/sys/cdefs.h" 2 3 4
- # 357 "/usr/include/features.h" 2 3 4
- # 388 "/usr/include/features.h" 3 4
- # 1 "/usr/include/i386-linux-gnu/gnu/stubs.h" 1 3 4
- # 1 "/usr/include/i386-linux-gnu/bits/wordsize.h" 1 3 4
- # 5 "/usr/include/i386-linux-gnu/gnu/stubs.h" 2 3 4
- # 1 "/usr/include/i386-linux-gnu/gnu/stubs-32.h" 1 3 4
- # 8 "/usr/include/i386-linux-gnu/gnu/stubs.h" 2 3 4
- # 389 "/usr/include/features.h" 2 3 4
- # 38 "/usr/include/assert.h" 2 3 4
- # 68 "/usr/include/assert.h" 3 4
- extern void __assert_fail (__const char *__assertion, __const char *__file,
- unsigned int __line, __const char *__function)
- __attribute__ ((__nothrow__)) __attribute__ ((__noreturn__));
- extern void __assert_perror_fail (int __errnum, __const char *__file,
- unsigned int __line,
- __const char *__function)
- __attribute__ ((__nothrow__)) __attribute__ ((__noreturn__));
- extern void __assert (const char *__assertion, const char *__file, int __line)
- __attribute__ ((__nothrow__)) __attribute__ ((__noreturn__));
- # 2 "macro.c" 2
- int main(int argc,char ** argv){
- int i=1;
- int j=2;
- int c;
- c = ( ((i ) > (j ))?(i ): (j ));
- ((j == 4) ? (void) (0) : __assert_fail ("j !== 4", "macro.c", 11, __PRETTY_FUNCTION__));
- ((2*(40 *80) == 6400) ? (void) (0) : __assert_fail ("2*(40 *80) == 6400", "macro.c", 12, __PRETTY_FUNCTION__));
- }
需要注意的是在宏扩展以后,asser宏已经被替换成了真正的条件表达式,不过该条件表达是比较奇怪,
- assert( j == 4)
- expand
- ((j == 4) ? (void) (0) : __assert_fail ("j == 4", "macro.c", 11, __PRETTY_FUNCTION__));
?表达式的条件部分很好立即,
条件结果为真是,执行的空操作 (void) (0) 这里看起来比较奇怪,其实也就是一个空操作
条件结果为假时 执行如下操作:
- __assert_fail ("j == 4", "macro.c", 11, __PRETTY_FUNCTION__)
该函数在宏扩展以后,我们已经看到了该函数的prototype,
compile and link and then run
对该程序进行编译,运行发现程序运行正常。
那C的结果为什么是4而不是3呢?这正是macro的诡异之处,
通过观察macro展开的代码不难发现
- c = ( ((i ) > (j ))?(i ): (j ));
不论那一个条件为真,较大的值都执行了两次自加操作,因此J的结果为4而不是3,
这一点要求我们在对两个函数的返回结果进行比较时要注意,
两个函数比较大小,函数总共执行3次,函数的大小对你程序的性能影响还是比较大的。
note
下面附上ASSERT.H的实现,感兴趣的筒子可以看一下。
something about assert.h
- /* Copyright (C) 1991,1992,1994-2001,2003,2004,2007
- Free Software Foundation, Inc.
- This file is part of the GNU C Library.
- The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- The GNU C Library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
- /*
- * ISO C99 Standard: 7.2 Diagnostics <assert.h>
- */
- #ifdef _ASSERT_H
- # undef _ASSERT_H
- # undef assert
- # undef __ASSERT_VOID_CAST
- # ifdef __USE_GNU
- # undef assert_perror
- # endif
- #endif /* assert.h */
- #define _ASSERT_H 1
- #include
- #if defined __cplusplus && __GNUC_PREREQ (2,95)
- # define __ASSERT_VOID_CAST static_cast
- #else
- # define __ASSERT_VOID_CAST (void)
- #endif
- /* void assert (int expression);
- If NDEBUG is defined, do nothing.
- If not, and EXPRESSION is zero, print an error message and abort. */
- #ifdef NDEBUG
- # define assert(expr) (__ASSERT_VOID_CAST (0))
- /* void assert_perror (int errnum);
- If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an
- error message with the error text for ERRNUM and abort.
- (This is a GNU extension.) */
- # ifdef __USE_GNU
- # define assert_perror(errnum) (__ASSERT_VOID_CAST (0))
- # endif
- #else /* Not NDEBUG. */
- #ifndef _ASSERT_H_DECLS
- #define _ASSERT_H_DECLS
- __BEGIN_DECLS
- /* This prints an "Assertion failed" message and aborts. */
- extern void __assert_fail (__const char *__assertion, __const char *__file,
- unsigned int __line, __const char *__function)
- __THROW __attribute__ ((__noreturn__));
- /* Likewise, but prints the error text for ERRNUM. */
- extern void __assert_perror_fail (int __errnum, __const char *__file,
- unsigned int __line,
- __const char *__function)
- __THROW __attribute__ ((__noreturn__));
- /* The following is not at all used here but needed for standard
- compliance. */
- extern void __assert (const char *__assertion, const char *__file, int __line)
- __THROW __attribute__ ((__noreturn__));
- __END_DECLS
- #endif /* Not _ASSERT_H_DECLS */
- # define assert(expr) \
- ((expr) \
- ? __ASSERT_VOID_CAST (0) \
- : __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
- # ifdef __USE_GNU
- # define assert_perror(errnum) \
- (!(errnum) \
- ? __ASSERT_VOID_CAST (0) \
- : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION))
- # endif
- /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
- which contains the name of the function currently being defined.
- This is broken in G before version 2.6.
- C9x has a similar variable called __func__, but prefer the GCC one since
- it demangles C function names. */
- # if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4)
- # define __ASSERT_FUNCTION __PRETTY_FUNCTION__
- # else
- # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
- # define __ASSERT_FUNCTION __func__
- # else
- # define __ASSERT_FUNCTION ((__const char *) 0)
- # endif
- # endif
- #endif /* NDEBUG. */
通过下面这个实验你可能还会进步一的了解macro的本质,
- ubuntu@ubuntu-virtual-machine:~/Desktop/detail$ cat a.h
- int a(int * a,int * b);
- ubuntu@ubuntu-virtual-machine:~/Desktop/detail$ cat b.h
- int b(int * a,int b) ;
- ubuntu@ubuntu-virtual-machine:~/Desktop/detail$ cat c.h
- int c(int a,int * b);
- ubuntu@ubuntu-virtual-machine:~/Desktop/detail$ cat ma.c
- #include "a.h"
- #include "b.h"
- #include "c.h"
- int main(int argc,char * argv]){
- return 0;
- }
- ubuntu@ubuntu-virtual-machine:~/Desktop/detail$ gcc -E ma.c
- # 1 "ma.c"
- # 1 ""
- # 1 ""
- # 1 "ma.c"
- # 1 "a.h" 1
- int a(int * a,int * b);
- # 2 "ma.c" 2
- # 1 "b.h" 1
- int b(int * a,int b) ;
- # 3 "ma.c" 2
- # 1 "c.h" 1
- int c(int a,int * b);
- # 4 "ma.c" 2
- int main(int argc,char * argv]){
- return 0;
- }
阅读(1715) | 评论(0) | 转发(0) |