Special usage of macros

Source: Internet
Author: User
1. Block "unused Parameters" Warning

This macro is used in ob_define.h in oceanbase to forcibly convert incoming data to void type.

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

This macro is mainly used to block the warning of "unused Parameters". For example, if the following function uses two parameters but none of them are used, some compilers will report a warning: P and mod_id are not used. All we useUNUSEMacro. In this way, both parameters are used, but no effective action is actually performed 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 byte alignment

By default, the compiler allocates space for each variable or data unit based on its natural limitations. Generally, you can use the following method to change the default peer condition:
1) Use pseudoinstructions#pragma pack (n), The compiler will be aligned according to n Bytes.
2) use pseudoinstructions#pragma pack ()To cancel the custom byte alignment.
In addition, the following method is provided:
3)__attribute__((aligned (n)))To align the structure members to the natural boundary of n Bytes. If the length of a member in the structure is greater than N, the maximum member length is used for alignment.
4)__attribute__ ((packed))Cancels the optimization alignment of the structure during compilation and alignment according to the actual number of bytes occupied.
The first method above n = 1, 2, 4, 8, 16... is more common.

#define CACHE_ALIGNED __attribute__((aligned(CACHE_ALIGN_SIZE)))
3. Variable Parameter macro

We specify that some function parameters are variable. For example, after the printf function is specified as the first formatted string, it can be followed by parameters with an indefinite number. Similarly, macros can also specify multiple parameters. There are two ways to do this.
First, useargs...And##argsSecond, use...And__VA_ARGS__. The following two macros are defined in two ways. Both macros can output a string before the printf string to 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:

P_Debug("%s:%d\n","good",100);P_Release("%s:%d\n","good",100);
4. #, ##, #@Difference

Assume that three macros are defined,

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

Then
A (1) ------> T_1
B (1) ------> '1'
C (1) ------> "1"

Welcome to my website-Butterfly's blog garden-a column with no name.
If you have any questions during reading this article, please contact the author. For more information, see the source!


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.