Two classic macros under Linux define "Go"

Source: Internet
Author: User

Transferred from: http://www.linuxidc.com/Linux/2015-07/120014.htm

This article first introduces Linux under the Classic macro definition, feel the wisdom of the geek, and then according to the classic definition for the next article to pave the groundwork.

OFFSETOF macro Definition:

Gets the offset of the variable member (MEMBER) of the struct body (TYPE) in this struct.
#define OFFSETOF (Type, MEMBER) ((size_t) & ((TYPE *) 0)->member)

Description: Gets the offset of the variable member (MEMBER) of the struct body (TYPE) in this struct.
1. ((TYPE *) 0) transforms 0 to a type pointer, that is, the address of a pointer of type types is 0.
2. ((TYPE *) 0)->member access to data members in the structure.
3.& (((TYPE *) 0)->member) takes out the address of the data member. Because the address of type is 0, the address obtained here is the offset of the relative member in type.
4. (size_t) (& ((type*) 0)->member) result conversion type. For 32-bit systems, size_t is the unsigned int type, and for 64-bit systems, size_t is the unsigned long type.

Examples of Use:

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;
}
Results:
/*
Gender_offset = 0
Id_offset = 4//byte on its
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.

CONTAINER_OF macro Definition:

#define CONTAINER_OF (PTR, type, member) ({\
Const typeof (((type *) 0)->member) *__mptr = (PTR); \
(Type *) ((char *) __mptr-offsetof (Type,member));})

Description: Gets a pointer to the entire struct variable, based on the pointer (PTR) of the domain member variable (member) in the struct (type) variable.
1. typeof (((type *) 0)->member) takes out the variable type of the member member.
2.const typeof (((type *) 0)->member) *__mptr = (PTR) defines the variable __mptr pointer and assigns PTR to __mptr. After this step, __mptr is a constant pointer to the member data type, which points to the address that PTR points to.
3. (char *) __MPTR converts the __mptr to a byte-type pointer.
4. Offsetof (Type,member)) is to get the position offset of "member member" in "struct type".
5. (char *) __mptr-offsetof (Type,member)) is the starting address (a char * type pointer) for the pointer that is used to get "struct type".
6. (Type *) ((char *) __mptr-offsetof (Type,member)) is the "pointer to struct type" of type "char *" to "pointer to struct type of type *".
7. Backslash "/" indicates row connection

for more details, please read on to the next page. Highlights : http://www.linuxidc.com/Linux/2015-07/120014p2.htm

Two classic macros under Linux define "Go"

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.