Choosing Between Functions and Macros
来源:互联网 发布:孕囊数据两个一样 编辑:程序博客网 时间:2024/06/05 15:57
FROM MSDN
Most Microsoft run-time library routines are compiled or assembled functions, but some routines are implemented as macros. When a header file declares both a function and a macro version of a routine, the macro definition takes precedence, because it always appears after the function declaration. When you invoke a routine that is implemented as both a function and a macro, you can force the compiler to use the function version in two ways:
- Enclose the routine name in parentheses.
#include <ctype.h>a = toupper(a); //use macro version of touppera = (toupper)(a); //force compiler to use function version of toupper
- “Undefine” the macro definition with the #undef directive:
#include <ctype.h>#undef toupper
If you need to choose between a function and a macro implementation of a library routine, consider the following trade-offs:
- Speed versus size. The main benefit of using macros is faster execution time.During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called. Macros may increase code size but do not have the overhead associated with function calls.
- Function evaluation. A function evaluates to an address; a macro does not. Thus you cannot use a macro name in contexts requiring a pointer. For instance, you can declare a pointer to a function, but not a pointer to a macro.
- Macro side effects. A macro may treat arguments incorrectly when the macro evaluates its arguments more than once. For instance, thetoupper macro is defined as:
#define toupper(c) ( (islower(c)) ? _toupper(c) : (c) )
In the following example, the toupper macro produces a side effect:
#include <ctype.h>int a = 'm';a = toupper(a++);
The example code increments
a
when passing it to toupper. The macro evaluates the argumenta++
twice, once to check case and again for the result, therefore increasinga
by 2 instead of 1. As a result, the value operated on byislower differs from the value operated on bytoupper. - Type-checking. When you declare a function, the compiler can check the argument types. Because you cannot declare a macro, the compiler cannot check macro argument types, although it can check the number of arguments you pass to a macro.
- Choosing Between Functions and Macros
- Choosing between Critical Sections and Mutex
- [IOS] The difference between functions and methods
- Inline Functions versus Macros
- What’s different between deferrable functions and work queues
- c++: Difference between member and non member functions
- Choosing Between ElasticSearch, MongoDB & Hadoop
- Choosing Between XML, Web Services, and Remoting for Rich Internet Applications
- XCode macros and tags
- Effective C#之20:Distinguish Between Implementing Interfaces and Overriding Virtual Functions
- between...and
- between ..... and ..........
- between...and..
- Choosing Quadcopter Motors and Props
- C++, macros, and job security
- about macros fs_initcall and module_init
- Choosing Between Forefront TMG or Forefront UAG for Publishing Scenarios
- Passing …(rest) parameters between functions
- 不懂C语言?你该感到危机了!
- DSP的基本特性(以TI的C54x为例)
- 小S:如果这世界上有你爱的人,你就没法自由
- shell 中括号 []、[[]]、(())
- WIZnet W5100和W5300的Linux驱动
- Choosing Between Functions and Macros
- 基于Davinci达芬奇系列DM6446的AVS-P2基准框架编码器系统框架优化设计
- flex图表中,经常用到的几种特效
- java设计模式之简单工厂模式
- error: No curses/termcap library found的解决办法
- jquery 操作iframe的几种方法
- 在ThinkPHP中开启调试模式
- 判断手机网络连接状态
- 1004