Using gnu c _ attribute _ Reading Notes

Source: Internet
Author: User
Using gnu c _ attribute _ Reading Notes

1. this article describes a feature of the GNU Compiler-_ attribute __. this macro is a compiler instruction. By defining this in the code, we can use the inform compiler to compile some logic of our code, in this way, the compiler can avoid some errors and improve performance at runtime. _ Attribute _ is useful in many codes. This is something we can see when we look at some more familiar code written by foreigners.

2. _ attribute _ format: This tells the Compiler which parameter is format string when our code processes functions such as printf and scanf that change parameters, which parameter is the parameter list to avoid some problems in the Code, such:

Code: select all
/* like printf() but to standard error only */
extern void eprintf(const char *format, ...)
   __attribute__((format(printf, 1, 2)));  /* 1=format 2=params */

/* printf only if debugging is at the desired level */
extern void dprintf(int dlevel, const char *format, ...)
   __attribute__((format(printf, 2, 3)));  /* 2=format 3=params */

We can see from the above that we have defined the eprintf function. The first parameter is format string, the second parameter is the list of parameters corresponding to format string, and the following dprintf is the same, the compiler can detect the following code errors:

Code: select all
$ cat test.c
1  extern void eprintf(const char *format, ...)
2               __attribute__((format(printf, 1, 2)));
3
4  void foo()
5  {
6      eprintf("s=%s\n", 5);             /* error on this line */
7
8      eprintf("n=%d,%d,%d\n", 1, 2);    /* error on this line */
9  }

$ cc -Wall -c test.c
test.c: In function `foo':
test.c:6: warning: format argument is not a pointer (arg 2)
test.c:8: warning: too few arguments for format

3. _ attribute _ noreturn indicates that the compiler does not return a function. I think it is of little use. Read the original English document of the attachment.

4. _ attribute _ Const. This tells the compiler that the function always returns the same value when a parameter is specified. This can help programs improve performance, for example:

Code: select all
extern int square(int n) __attribute__((const));
...
   for (i = 0; i < 100; i++ )
   {
      total += square(5) + i;
   }

If _ attribute _ const is not defined in the square function, the program generates a code to call the square function every time in the following loop. However, after the const is specified, the program will know that for the same input parameter 5, the return values are the same. In this way, the program will execute Square once, and then cache the return value of the function. As a result, the call to the square function will no longer have the logic of the function call, and the last result will be returned directly.

5. You can mix the preceding two writing methods:

Code: select all
/* send printf-like message to stderr and exit */
extern void die(const char *format, ...)
   __attribute__((noreturn))
   __attribute__((format(printf, 1, 2)));

/*or*/

extern void die(const char *format, ...)
   __attribute__((noreturn, format(printf, 1, 2)));

6. program portability caused by _ attribute _, because _ attribute _ is dedicated to the GNU Compiler, so what if we need to port such code to a non-GNU environment? It's easy to use Conditional compilation:

/* If we're not using gnu c, Elide _ attribute __*/
# Ifndef _ gnuc __
# DEFINE _ attribute _ (x)/* nothing */
# Endif

In this example, define _ attribute _ (x) is displayed? _ Attribute _ only accepts one parameter, but we can see that there are several parameters when using format and Const? When _ attribute _ is used, there is a bracket outside the keyword format and Const. That is to say, we are inside every _ attribute, the two-layer brackets are used, including the format and const, which indicate that the content in the brackets is a parameter, so that it complies with the definition here. Note: This method is also common on Windows platforms. It should be said that this is a very common technique in C language programming, especially macro programming, you can define an indefinite parameter as a macro type with only one parameter.

7. Note:

When using _ attribute _, note that we must use _ attribute __in the function declaration. In the function implementation, it cannot be defined-that is, for every function that wants to use the _ attribute _ feature, a function declaration must be manually defined, followed by the function implementation, it is not possible to write a function, because _ attribute _ cannot be defined in the function implementation (the gnu c compiler does not know why). For example:

Code: select all
/* function declaration */
void die(const char *format, ...) __attribute__((noreturn))
                                  __attribute__((format(printf,1,2)));

void die(const char *format, ...)
{
   /* function definition */
}

Related Article

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.