Some special-purpose macros

Source: Internet
Author: User

1. Block unused parameters warning

Such a macro was used in Ob_define.h in oceanbase to force the incoming data into void type.

   #ifndef UNUSED    #define UNUSED (v) ((void) (v))    #endif


Such a macro is primarily intended to mask the "unused parameters" warning, as the following function uses two parameters. However, some compilers will report a warning when none is used: they feel that P and mod_id are unused.

All of us use UNUSE macros so that two of the parameters are used, but we don't actually do anything effective to block the "unused parameters" warning.

virtual void Mod_free (void* p, const int32_t mod_id = 0) {     UNUSED (p);     UNUSED (mod_id);  }


2. Specify the byte alignment

In the default case. The compiler allocates space for each variable or data unit according to its natural boundary conditions. In general. The default boundary conditions can be changed by the following methods:
1) Use pseudo #pragma pack (n) -directives. The compiler will align to n bytes.


2) use pseudo #pragma pack () -directives to cancel your own definition of byte alignment.


In addition, there is one way to do this:
3) __attribute__((aligned (n))) . Aligns the members of the structure to the N-byte natural boundary. Assume that there are members in the structure that are longer than N. Is aligned according to the length of the maximum member.


4) __attribute__ ((packed)) , the optimization of the structure in the compilation process alignment, according to the actual number of bytes to align.


Above n = 1, 2, 4, 8, 16 ... The first approach is more common.

#define Cache_aligned __attribute__ ((ALIGNED (cache_align_size))


3. variable parameter Macros

We specify that some functions have variable parameters, such as the printf function. Specifies that the first is a formatted string after. The following can be the same as the variable number of parameters. A macro can also specify more than one number of parameters. There are two ways to do this.
One is args... to use and ##args , two is to use ... and __VA_ARGS__ . The following are two macros defined in two ways. Both macros can be in front of the printf string. Output a string first. Achieve the same effect.

#define P_DEBUG (Format,args ...) {    printf ("[DeBug]");    printf (format,# #args);} while (false) #define P_RELEASE (format,...) \do{    printf ("[Release]");    printf (format,__va_args__);} while (false)


When used as:

P_debug ("%s:%d\n", "good", 100); P_release ("%s:%d\n", "good", 100);


4. ###, #@The difference

If you define a 3 macro,

#define A (x) t_# #x # define B (x) #@x#define C (x) #x


The following expands to
A (1)------>t_1
B (1)------> ' 1 '
C (1)------> "1"

Welcome to My Site----butterfly suddenly the blog park----People are both nameless column.
If you have any questions about reading this article,

T=qm_mailme&email=jx4wfx8eex4xe2dwvgleseo "style=" background-color:transparent "> Contact author, reprint please specify the source!


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Some special-purpose macros

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.