C language Offsetof macros and CONTAINER_OF macros

Source: Internet
Author: User

First, we need to understand that when you access individual elements in a struct through struct variables, they are essentially

accessed by pointers, but this time the compiler helped us calculate each

The offset between the element and the start address of the struct.


One: Offsetof macro:

#define OFFSETOF (Type, MEMBER) ((int) & ((TYPE *) 0)->member)

1: Parameter and return value analysis:

(1) Type is a struct type, member is the element name of an element in a struct

(2) This macro returns the offset of the member element relative to the first address of the entire struct variable, and the type is int

2: Acting on the principle

(1) The function of the Offsetof macro is to use a macro to calculate the offset of an element and the first address of a struct in a struct.

Volume (which is essentially a compiler to help us calculate).

(2) Offsetof macro principle: We dummy a type struct variable, and then use Type.member

Way to access the member element, and then get the offset of the member relative to the first address of the entire variable.

3: From the combination of ways to analyze:

(type *) 0 This is a coercion type conversion that converts a 0 address coercion type to a pointer, which refers to the

A struct-body variable to a type. (In fact, this struct variable may not exist, but as long as I

Do not dereference this pointer without error).


((type *) 0)->member (type *) 0 is a pointer to a type struct variable, using the pointer pointer

To access the member element of this struct variable


& ((Type *) 0)->member equivalent to & (((type *) 0)->member), meaning is to get MEMBER yuan

The address of the vegetarian. But since the first address of the entire struct variable is 0, the address of the resulting member element

Is the offset of the member element.


Two: CONTAINER_OF macro

#define CONTAINER_OF (PTR, type, member) ({Const typeof (((type *) 0)->member) * __mptr = (PTR);(type *) ((char *) __mptr- Offsetof (type, member)); Note: \ is a line break here

1: Parameter and return value analysis:

(1) PTR is a pointer to a struct element member, type is a struct, member is a struct

The element name of an element in

(2) This macro returns a pointer to the entire struct variable, which is (type *)

2: Function and Principle analysis:

(1) Function: A pointer to an element in a struct, and a pointer to the struct variable. Yes

The CONTAINER_OF macro, we can get pointers to the entire struct variable from the pointer of an element, and then

Get pointers to other elements in the struct.

(2) The function of the TypeOf keyword is: Typepof (a) is the type of a given by variable A, typeof is the

Variable name gets the variable data type.

(3) How this macro works: First use typeof to get the type of member element is defined as a pointer, then

This pointer subtracts the offset of the element relative to the entire struct variable (the offset is used by the OFFSETOF macro

), minus the first address of the entire struct variable, and then forcing the address to type

Convert to Type *.













C language Offsetof macros and CONTAINER_OF macros

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.