Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Two classic macro definition under Linux     - How to use Xmanager Remote Desktop and VNC Log (Linux)

- I like Linux Security (Linux)

- Oracle GoldenGate encryption (Database)

- Spacewalk Linux system configuration and installation (Linux)

- How do you know your public IP terminal in Linux (Linux)

- SLF4J Tutorial (Programming)

- CentOS 6.5 installation VNCServer implement graphical access (Server)

- Installation JDK 1.8 under CentOS7 (Linux)

- About enhanced Linux / Unix server system security program (Linux)

- Grading defense against Linux server attacks (Linux)

- osprofiler use OpenStack Cinder Lane (Server)

- apt-get and apt-cache show command examples (Linux)

- Linux platform to prevent hackers to share practical skills (Linux)

- Simple Linux file system - df, du, ln (Linux)

- The Java utility, JavaMail (Programming)

- How apt-get limited use IPv4 or IPv6 protocol to download (Linux)

- Restore Oracle Database Cold backup and database reconstruction emca (Database)

- C + + secondary pointer memory model (pointer array) (Programming)

- Text editing and viewing text Linux command (Linux)

- MySQL database master never solve the synchronization method (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.
- MySQL primary and secondary replicate data inconsistencies (Database)
- Iptables use examples (Linux)
- Ten minutes to find out the character set and character encoding (Linux)
- Linux upgrade Glibc (Linux)
- CentOS 6.5 installation configuration DRBD (Server)
- Ubuntu and derivatives installation Atom 0.104.0 (Linux)
- CentOS install Memcached (Server)
- How to set the default Fedora from the command line (Linux)
- Ubuntu users to install Gnome-Pie 0.6.5 (Linux)
- Advanced Search Oracle study notes (Database)
- Linux Timing task Crontab command Detailed (Linux)
- Simple and fast sorting (Programming)
- C language files update in real time (Programming)
- Linux-- sub-volume compression and decompression (Linux)
- Echo Command Examples (Linux)
- ORA-12545: Connection failed because the target host or object does not exist (Database)
- Linux boot the system does not display a progress bar setting method (Linux)
- Apache2.4.7 make an error [exports.lo] Error 1 Solution (Server)
- How to enhance the Nagios server security (Linux)
- Some problems and countermeasures Linux system calls exist (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.