Calculate the address of the struct Variable Based on the address of the member variable

Source: Internet
Author: User
When writing a C program, we sometimes need to get the structure address based on the address of the structure member variable, especially when we want to use C to implement the inheritance feature of C ++.
The problem is analyzed as follows:
  • Input: a struct defines the type. The name of a member variable in this struct is member and its address is PTR.
  • Output: the address of the struct containing this member variable.

For ease of analysis, we provide an example to describe

Struct father_t {
Int;
Char * B;
Double C;
} F;
Char * PTR = & (f. B );
// Instead of PTR = f. B; PTR is the address of B, not the address it points.

Based on the storage features of the struct type in C language, we can draw an illustration as follows:

Through the analysis graph, we can see that we only need to know the member variable address PTR, minus it in the structure of the relative offset 4 to the structure address (ptr-4 ).
In Linux, there is a good macro that can be used, called container_of, in Linux/kernel. h. Its definition is as follows:

/**
* Container_of-cast a member of a structure out to the Containing Structure
*
* @ PTR: the pointer to the member.
* @ Type: the type of the container struct this is embedded in.
* @ Member: the name of the member within the struct.
*
*/
# Define container_of (PTR, type, member )({
Const typeof (type *) 0)-> member) * _ mptr = (PTR );
(Type *) (char *) _ mptr-offsetof (type, member ));})

# Define offsetof (type, member) (size_t) & (type *) 0)-> Member)

The above definition is analyzed as follows:

  1. (Type *) 0-> member is designed as a Type type struct, the starting address is 0, the compiler adds the starting address of the struct to the offset of the member variable of the struct to get the offset address of the variable volume of the struct. Because the starting address of the struct is 0, therefore, the offset address of the struct member variable is equal to the offset of the member variable from the start part of the struct in the structure. That is, & (type *) 0-> member) is the offset address of the member variable. And it is equal to the offset in the Structure Body: (size_t) (& (type *) 0)-> member) after size_t forced type conversion, the value is the offset of the structure body. The offset is obtained by offsetof.
  2. Typeof (type *) 0)-> member) is the variable type used to retrieve members of member. Use it to define the _ mptr pointer. PTR is the pointer to the member variable. _ Mptr is a constant pointer of the member data type, pointing to the variable pointed to by PTR.
  3. (Char *) _ mptr to byte pointer. (Char *) _ mptr-offsetof (type, member) is used to find the starting address of the struct (char * pointer), and then (type *) (char *) _ mptr-offsetof (type, member) converts the starting pointer of a byte struct to the starting pointer of A type * struct under the action of (type.

This is the first pointer from a member variable pointer of the struct. The pointer type is converted from a member variable type of the struct to the struct type.

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.