Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Two classic macro definition under Linux     - The oh-my-zsh into true my zsh (Linux)

- Iptables principle (Linux)

- redis main building and disaster recovery from a cluster deployment (Database)

- Example of use WebSocket (Programming)

- Linux dd command make U disk boot disk (Linux)

- Polymorphism of the C ++ compiler and run-time polymorphism (Programming)

- Hazelcast integration with MongoDB (Database)

- Java class loading order (Programming)

- FastDFS installation and deployment (Server)

- AWK principles and commands and file input (Linux)

- How to choose the first programming language based on the life you want (Programming)

- Linux command execution judge sentences -;, &&, || (Linux)

- Crawl use Wireshark packet on remote Linux (Linux)

- Oracle 10046 Event (Database)

- Use 3G wireless network card under Linux (Linux)

- Ubuntu installed Gimp 2.6.11 (stable version) with PPA (Linux)

- Detailed LVM2 (Linux)

- Linux environment installation of rvm and ruby (Linux)

- Win7 + Ubuntu Kylin + CentOS 6.5 installed three systems (Linux)

- RedHat Redis Linux installation (Database)

  Two classic macro definition under Linux
  Add Date : 2016-11-04      
  This paper introduces the classic Linux under the macro definition, feeling geeks wisdom, then according to the classical definition to pave the way for the next article.

offsetof macro definitions:

// Get structure (TYPE) of the variable elements (the MEMBER) In the structure of the offset.
#define offsetof (TYPE, MEMBER) ((size_t) & ((TYPE *) 0) -> MEMBER)

Description: Get structures (TYPE) member variables (MEMBER) In this structure the offset.
1. ((TYPE *) 0) the transition to zero TYPE pointer type, that type TYPE pointer address is 0.
2 ((TYPE *) 0) -.> Data member MEMBER access structure.
3 &. (((TYPE *) 0) -> MEMBER) fetch address data members. Since the TYPE address is 0, the address is here to get the relative offset TYPE MEMBER of.
4. (size_t) (& (((TYPE *) 0) -> MEMBER)) result of the conversion type. For 32-bit systems, size_t is unsigned int type; For 64-bit systems, size_t is unsigned long type.

Example 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;
/ *
gender_offset = 0
id_offset = 4 // its bytes
age_offset = 8
name_offset = 12
* /

TYPE is the structure, which represents the "whole"; and MEMBER is a member, which is a part of the whole.

container_of macro definitions:

#define container_of (ptr, type, member) ({\
    const typeof (((type *) 0) -> member) * __ mptr = (ptr); \
    (Type *) ((char *) __ mptr - offsetof (type, member));})

Description: According to the "structure (type) variable" in the "domain member variables (member) of the pointer (ptr)" to obtain a pointer to the entire structure variables.
1. typeof (((type *) 0) -> member) Remove the member of the member variable types.
2.const typeof (((type *) 0) -> member) * __ mptr = (ptr) define variables __mptr pointer and ptr assigned to __mptr. After this step, __ mptr data type for the member of a constant pointer, which points to the address pointed to by ptr.
3. (char *) __ mptr __mptr will be converted to a byte pointer.
4. offsetof (type, member)) is to get the "member members" in the "structure type" location offset.
5. (char *) __ mptr - offsetof (type, member)) is used to obtain the starting address "structure type" pointer (a char * pointer).
6. (type *) ((char *) __ mptr - offsetof (type, member)) is to "char * type of structure type pointer to" convert "type * type of structure type pointer."
7. slash "/" indicates that the line connection

Example of use:
Note: this example addressof macro definitions and macro definitions above, except that a rewrite.

// Get structure (TYPE) of the variable elements (the MEMBER) In the structure of the offset.
#define offsetof (TYPE, MEMBER) ((size_t) & ((TYPE *) 0) -> MEMBER)
/ * According to "structure (type) variable" in the "domain member variables (member) of the pointer (ptr)" to obtain a pointer to the entire structure variables * /
#define addressof (PT, TYPE, MEMBER) ((TYPE *) ((char *) (PT) - offsetof (TYPE, MEMBER)))

struct student
 char gender;
 int id;
 int age;
 char name [20];

void main ()
 struct student stu;
 struct student * pstu;

 stu.gender = '1';
 stu.id = 9527;
 stu.age = 24;
 strcpy (stu.name, "abc");

 // According to "id address" get "the address of the structure."
 pstu = addressof (& (stu.id), struct student, id);

 // According to the acquired address of the student body structure, access to other members
 printf ( "gender =% c \ n", pstu-> gender);
 printf ( "age =% d \ n", pstu-> age);
 printf ( "name =% s \ n", pstu-> name);
gender = 1
age = 24
name = abcgender = 1
age = 24
name = abc
* /

type structure is, it represents the "whole"; and the member is a member, which is a part of the whole, and the address of the member is known.

Finally, you can think about what are the macro uses.
- ARM constant expression (Programming)
- The Java way to stop a thread of execution (Programming)
- Use Mop monitor stock prices at the Linux command line (Linux)
- Cacti installation deployment under CentOS 6.6 (Server)
- Linux permissions Detailed (Linux)
- Ubuntu deployed under regular tasks with crontab (Linux)
- Hanoi problem Java Solution (Programming)
- Ubuntu install virtual machine software VirtualBox 4.3.10 (Linux)
- Oracle 11gr2 new APPEND_VALUES tips (Database)
- Analysis of C ++ exception mechanism (Programming)
- CentOS 7 virt-manager can not connect a local hypervisor (Linux)
- Transplant spider to MySQL 5.6 (Database)
- MySQL primary and secondary replicate data inconsistencies (Database)
- Summary Linux operating system some tips to prevent attacks (Linux)
- How to use the Linux kill command to kill the process / program is not responding (Linux)
- The method of MySQL two kinds of incomplete recovery (Database)
- GAMIT 10.50 installed in Ubuntu 12.04 system (Linux)
- Hive start being given: Found class jline.Terminal, but interface was expected (Database)
- Upgrade to Node V4 seven reasons (Programming)
- Java Timer (regular calling, to achieve a fixed time to perform) (Programming)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.