Gnu c-using GNU gcc _ attribute _ mechanic 02 variable attribute & type attribute

Source: Internet
Author: User

Part 1ArticleIntroduced in _ attribute _ mechanic function attribute reference: http://www.cnblogs.com/respawn/archive/2012/07/09/2582548.html

 

I do not like to make too many modifications to my own articles, so I do not pay much attention to typographical work and like to take a simple line. So if you think that my blog is poorly formatted and ignore the content of the article, then I

It can only be said that I focus on technology, not on the secondary waste of time and non-value actions. if you can correct my blog post, I would like to thank you. technology, more discussions and more

The research is not written for praise. (The above only represents my personal opinion. If you do not like it, please forgive me .)

 

I had to go out in the afternoon and took a bath in the dormitory just now. Now I listened to the music and calmed down to continue my unfinished work.

 

In the previous article, I provided a lot of instance operations. in this article, I will no longer give examples, because the methods are clear. you can try it by yourself. If one or two instances do need to be provided

, I will provide the reference instance.

 

1. Variable attribute

1. _ attribute _ (alias): As mentioned in the previous article, limitations on functions are defined here. The usage of variables is similar:

 
Type newname _ attribute _ (alias ("oldname ")));

 

Newname and oldname do not need to be explained again. As mentioned in the previous article, the newname operation is the oldname operation.

 

2. _ attribute _ (aligned (alignment): specifies the minimum number of bytes of the variable or struct, in bytes.

Alignment: Specifies the bytes alignment operand.

I will not talk about anything else, because if you need to describe the content of the byte alignment part, you can write a blog article separately. If you have time, I will supplement this section, write an article separately.

 

3. _ attribute _ (cleanup (cleanup_function): When the scope of a variable disappears, the clean_function function will be executed.

How can we understand this? Just like the smart pointer in C ++, it will be destroyed when it leaves its scope. It can also be done by using the _ attribute mechiansm cleanup attribute.

To.

 

4. _ attribute _ (deprecated (MSG): indicates that this is a discarded variable. As mentioned above, it is a discarded function.

MSG: output information.

If the discarded variable is used elsewhere, the compiler will output warnning. however, compilation will still pass. the warnning information includes the unknown information about the use of the obsolete variable, usually

This type of information in a file.

The usage method is the same as the method for discarding a function.

 

5. _ attribute _ (Section ("section-name"): This is what I mentioned earlier.

 

6. _ attribute _ (packed): Make the variable or struct alignment in the smallest way. The variable is 1 byte alignment, and the field, that is, the field indicates bit alignment.

This will be put behind the discussion with the previous aligned as the byte aligned.

 

7. _ attribute _ (weak): This is very tricky. The manual on the official website only loses a link, which is to refer to the usage of weak attribute in function attribute. so I will not talk much about it.

 

 

Ii. Type attribute

1. _ attribute _ (aligned (alignment): byte alignment, which is similar to the previous usage. Note that when aligned attribute is used to align variables or structures in bytes, after alignment, it will only increase or remain unchanged.

 
Struct s {short f [3];} _ attribute _ (aligned (8 ))); typedef int more_aligned_int _ attribute _ (aligned (8 )));

 

As shown above, struct s specifies that the alignment mode is 8 byte. f [3], which is a total of 6 bytes. Because the specified byte alignment operand is 8, the entire s struct is 8 bytes.

 

2. _ attribute _ (packed): packed attribute has been introduced earlier. unlike the preceding aligned, packed adopts a compact byte alignment mode, so it is executed in the last step according to the minimum aligned mode.

Struct unpacked_struct {char C; int I ;}; struct _ attribute _ (packed) packed_struct {char C; int I; struct unpacked_struct us ;};

 

Example aboveCode, We can see that there is a struct unpacked_struct object with no alignment specified in struct packed_struct with the packed alignment specified.

Although the structure packed_struct is aligned in the packed mode, the specified bytes of its member us cannot be aligned. this is important, so if you need to fully compact the packed_struct structure,

The packed mode of unpacked_struct needs to be compact and aligned.

 

 

 

OK. I have completed the remaining tasks in the morning...

 

 

 

 

 

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.