Embedded Linux C Language (vii)--structure

Source: Internet
Author: User

Embedded Linux C Language (vi)--structure I. Introduction to the structure of the body1. definition of structural body

There are two common approaches to structure definition:

The first method:

struct person{

Char *name;

unisgned int age;

};

The second method:

typedef struct person{

Char *name;

unsigned int age;

}person;

The person instance is declared as follows:

Person person;// declares a person Object

Person *ptrperson = (person*) malloc (sizeof);// declares a person object and allocates memory

2. initialization of the struct body

Initialization of a struct object declared using struct body:

person person;

person.name = (char *) malloc (strlen ("socprio") + 1);

strcpy (person.name, "Scorpio");

Person.age = 30;

Initialization of struct bodies declared using struct pointers:

Person *ptrperson;

Prtperson = (person *) malloc (sizeof (person));

ptrperson->name = (char *) malloc (strlen ("Scorpio") + 1);

strcpy (ptrperson->name, "Scorpio");

Prtperson->age = 30;

3. problems with structural release

When a struct is defined, memory is allocated to the struct, but the runtime does not automatically allocate memory for pointers inside the struct, and the runtime system does not automatically release the memory that is pointed to by the pointer inside the struct when the struct is destroyed. The pointer inside the struct points to memory, which is typically dynamically allocated, and needs to be released after use, or it will cause a memory leak.

4. avoidance of dynamically allocating memory overhead

Recurring allocations and then releasing the fabric will incur overhead, which can lead to huge performance bottlenecks. The porting method to solve this problem is to maintain a single table for the allocated structure. When the user does not need a struct instance, it is returned to the struct pool. Gets an object from the struct pool when a struct instance is needed. If no struct is available in the struct pool, a struct instance is automatically assigned dynamically.

Second, struct Macro 1. Offsetof Macro

Offsetof calculates the offset of the member from the first address of the struct, based on the type and member name of the struct member. The macro is defined as follows:

#define OFFSETOF (Type, member) ((size_t) (& ((type *) 0)->member))

((TYPE *) 0) : 0 address coercion " convert " to a pointer of type struct types

((TYPE *) 0)->member : Accessing member data members in the type structure

& (((TYPE *) 0)->member) : Remove the address of the data member member in the type structure

(size_t) (& ((type*) 0)->member ) : result converted to size_t type

The OFFSETOF macro First converts 0 to the struct pointer type, and then references the member variable and takes its address. Because the first address of the struct is 0, the address of the member variable is the offset of the member from the first address of the struct body.

Common in modern compilers

#define OFFSETOF (type, member) __builtin_offsetof (type, member)

2. container_of Macro

Container_of calculates the first address of a struct based on its member address, struct type, and member name, as defined below:

#define CONTAINER_OF (PTR, type, member) ({\

Const typeof (((type *) 0)->member) *__mptr = (PTR); \

(Type *) ((char *) __mptr-offsetof (Type,member));})

Create a struct with offsetof and container_of to get the address of the struct and the offset of the internal member relative to the structure's address.

code example:

#include <stdio.h>

#define OFFSETOF (Type, member) ((size_t) & ((type *) 0)->member)

#define CONTAINER_OF (PTR, type, member) ({\

Const typeof (((type *) 0)->member) *__mptr = (PTR); \

(Type *) ((char *) __mptr-offsetof (Type,member));})

struct test{

Char A;

int b;

Short C;

Char *p;

Double D;

}__ATTRIBUTE__ ((Aligned (4)));

int main (int argc, char **argv)

{

struct test S;

printf ("Offset a=%lu\n", offsetof (struct test,a));

printf ("Offset b=%lu\n", offsetof (struct test,b));

printf ("Offset c=%lu\n", offsetof (struct test,c));

printf ("Offset p=%lu\n", offsetof (struct test,p));

printf ("Offset d=%lu\n", offsetof (struct test,d));

printf ("S=%p\n", container_of (&s.a,struct test,a));

printf ("S=%p\n", container_of (&s.p,struct test,p));

return 0;

}

Operation Result:

Offset a=0

Offset b=4

Offset c=8

Offset p=16

Offset d=24

S=0x7fffc8590cf0

S=0x7fffc8590cf0



This article from "Endless life, Struggle not only" blog, please be sure to keep this source http://9291927.blog.51cto.com/9281927/1790670

Embedded Linux C Language (vii)--structure

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.