简体   繁体   中英

Round and curly bracket block of code in C

Can anyone explain what this macro evaluates to:

#define memcpy(dest,src,n) ({ \
void * _res = dest; \
__asm__ ("cld;rep;movsb" \
    ::"D" ((long)(_res)),"S" ((long)(src)),"c" ((long) (n)) \
    :"di","si","cx"); \
_res; \
})

This is taken from the first version of Linux kernel, but I am wondering what does a block of code surrounded by this ({ }) represent and where would it be used?

A compound statement in parentheses is a GCC extension called a statement expression. It allows you to include declarations, for loops etc where an expression is expected. The last thing in the compound statement should be an expression followed by a semicolon which serves as the value of the entire construct.

The clang compiler also has support for them.

They are mainly just a convenience to remove the need to write lots of small functions that are only used once but are also used to prevent problems with macros when a term appears more than once; for example:

#define maxint(a,b) \
       ({int _a = (a), _b = (b); _a > _b ? _a : _b; })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM