Two classic macro definitions in Linux

Source: Internet
Author: User

Two classic macro definitions in Linux

This article first introduces the classic macro definition in Linux, feels the wisdom of geeks, and then paves the way for the next article based on the classic definition.

Offsetof macro definition:

// Get the offset of the variable MEMBER (MEMBER) of the struct (TYPE) in this struct.
# Define offsetof (TYPE, MEMBER) (size_t) & (TYPE *) 0)-> MEMBER)

Note: Get the offset of the variable MEMBER (MEMBER) of the struct (TYPE) in this struct.
1. (TYPE *) 0) converts zero to TYPE pointer, that is, the address of TYPE pointer is 0.
2. (TYPE *) 0)-> data MEMBER in the MEMBER access structure.
3. & (TYPE *) 0)-> MEMBER) retrieves the address of the data MEMBER. Because the TYPE address is 0, the obtained address is offset from the member type.
4. (size_t) (& (TYPE *) 0)-> MEMBER) result conversion TYPE. For 32-bit systems, size_t is of the unsigned int type; for 64-bit systems, size_t is of the unsigned long type.

Example:

Struct student
{
Char gender;
Int id;
Int age;
Char name [20];
};


Int _ tmain (int argc, _ TCHAR * argv [])
{
Int gender_offset, id_offset, age_offset, name_offset;


Gender_offset = offsetof (struct student, gender );
Id_offset = offsetof (struct student, id );
Age_offset = offsetof (struct student, age );
Name_offset = offsetof (struct student, name );


Printf ("gender_offset = % d \ n", gender_offset );
Printf ("id_offset = % d \ n", id_offset );
Printf ("age_offset = % d \ n", age_offset );
Printf ("name_offset = % d \ n", name_offset );


System ("pause ");
Return 0;
}
// Result:
/*
Gender_offset = 0
Id_offset = 4 // byte
Age_offset = 8
Name_offset = 12
*/

Offsetof Diagram

TYPE is a struct, which represents "whole", while MEMBER is a MEMBER, which is a part of the whole.

Iner_of macro definition:

# Define container_of (ptr, type, member )({\
Const typeof (type *) 0)-> member) * _ mptr = (ptr );\
(Type *) (char *) _ mptr-offsetof (type, member ));})

Note: The pointer to the entire struct variable is obtained based on "ptr of the domain member variable (member)" in "struct (type) variable.
1. typeof (type *) 0)-> member) retrieves the variable type of the member.
2. const typeof (type *) 0)-> member * _ mptr = (ptr) defines the Variable _ mptr pointer and assigns ptr to _ mptr. In this step, __mptr is a constant pointer of the member data type, pointing to the address pointed to by ptr.
3. (char *) _ mptr converts _ mptr to a byte pointer.
4. offsetof (type, member) is to get the location offset of "member" in "struct type.
5. (char *) _ mptr-offsetof (type, member) is the starting address (char * type pointer) used to obtain the pointer of the "struct type ).
6. (type *) (char *) _ mptr-offsetof (type, member )) is to convert "char * type struct type Pointer" to "type * type struct type Pointer ".
7. Backslash "/" indicates row connection

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.