Simple to use C language macros

Transfer from: http: //blog.sina.com.cn/s/blog_861912cd0100tc94.html

 

To write the C language, nice macro definition is very important. Macro definitions can help us prevent errors, improve portability and readability of the code and so on.

  In the software development process, often have some common or generic functions or code segments, these functions may be written as a function of both, it may be encapsulated into a macro definition. So what is a good function, or define a macro? This requires us to be both reasonable trade-off.

  We look at an example, compare two numbers or expressions size, first we write it as a macro definition:

  #define MAX( a, b) ( (a) > (b) (a) : (b) )

  Secondly, it implemented as a function:

  int max( int a, int b)

  {

  return (a > b a : b)

  }

  Obviously, we do not choose to use the function to accomplish this task, there are two reasons: First, the function call will bring additional overhead, it needs to open up a stack space, record the return address, the parameter push, returned from the function but also the release of the stack. This will not only reduce the overhead of code efficiency, and will greatly increase the amount of code, and then use the macro definition function even better than in code size and speed; Second, the parameters of the function must be declared as a specific type, so it can only be used on the appropriate type of expression, if we want to compare two floating-point type size, you have to write a specific type of floating-point comparison function. Conversely, above that may be used for shaping the macro definition, long integer, single float, double floating point and may be any other ">" comparison operator type size values, i.e., independent of the type of macro is .

  And using the function compared to the use of macros is disadvantageous in that every macro use, a macro definition codes are inserted into the copy program. Unless the macro is very short, otherwise it will be a substantial increase in the use of the length of the macro program.

  There are some tasks can not use function to achieve, but with the macro definition is very good implementation. Parameters such as the type not passed to the function as a parameter, but the parameter type to a band pass macro argument.

  See the examples below:

  #define MALLOC(n, type) \

  ( (type *) malloc((n)* sizeof(type)))

  Using this macro, we can think that we have any type of assignment for a specified amount of space, and returns a pointer to this space. We can look at this precise macro work process:

  int *ptr;

  ptr = MALLOC ( 5, int );

  After the results of these macro expansion:

  ptr = (int *) malloc ( (5) * sizeof(int) );

  This is one of the classic examples of the application of macro definitions to complete the function can not complete the function, but macros can not be abused, usually, if the same code needs to appear in several places in the program, a better approach is to implement it as a function.

  The following summary and differences macros and functions for all to use when writing code, this summary is taken from "C and pointers," a book.

 

 

define one-line definition of
example:

Maxi #define (A, b) (A>; b A:? b)
define multi-line definitions

define can replace multiple lines of code, such as macro definition in MFC (very classic, although people looked disgusting)

# the mACRO dEFINE (arg1, arg2) do {\
   \
stmt1; \
stmt2; \
   \
} the while (0)  
is essential to add one each time a new line "\"


// write macros swap (x, y) swap function
#define the swap (X, Y) \
X = X + Y; \
Y = X - Y; \
X = X - Y;


ZigBee define a plurality of rows in the following examples

#define FillAndSendTxOptions (TRANSSEQ, ADDR, ID , LEN , TXO) {\
afStatus_t STAT; \
ZDP_TxOptions = (TXO); \
STAT = fillAndSend ((TRANSSEQ), (ADDR), (ID), (LEN)); \
ZDP_TxOptions = AF_TX_OPTIONS_NONE; \
return stat;                                        \
}

Guess you like

Origin www.cnblogs.com/wrkk/p/11236806.html