An important data structure in linux C-struct

Source: Internet
Author: User

As we all know, Linux kernel is implemented by C language and a small part of Assembly. How can we ensure the clarity and correctness of the system organization in such a large system with a process-oriented C language? In fact, although the C language does not encapsulate the data structure of objects, it has a more flexible thing-the organization body. This guy is engaged in a hundred battles and can be stretched out. He plays the role of an object everywhere in the kernel.

Struct, as its name implies, is structured to organize some data structures. Therefore, objects need to encapsulate data and operations and are only used to organize data structures. What about the function? You ignore the powerful C language because of its pointer. Skilled application pointers can increase the program's efficiency by an order of magnitude. However, its security is the source of java abandonment. Pointer is one of the basic data structures of C, and struct is used to organize the basic data structure to form an encapsulation structure that meets application requirements. Of course, it can contain pointer elements. In addition, c supports function pointers, so struct has no reason not to include function pointers. Therefore, struct encapsulates data-related operations by including function pointers, this completes the abstraction of the "object" data structure at the C language layer. Really powerful.

Okay. If you know the structure of ox X, you need to master its application. Our goal is to master the most awesome things, so that you can get yourself up. Even if we are still a little white, a small dish, and more accumulation, more precipitation. Xiao Bai will also capture chicken and vegetables and thrive ...... Struct still has some application skills, because it is the organization and encapsulation of the basic data structure, and the data structure, data, memory, and memory allocation are involved. Memory Allocation involves alignment. to optimize our program, you must understand the byte alignment problem in struct:
You must read this article, "[byte alignment]. It is your loss.

The following is the priority of Some Operators related to struct found during my Blog reading today. This is rare. The following content must be explained below:
[Original address]

View plain
Among all operators, the following four operators have the highest priority: struct operator ". "and"-> "," () "used for function calls, and" [] "used for the lower mark. Therefore, they are the most closely associated with operands.
 
For example:
 
Struct {
Int len;
Char * str;
} * P;
 
Expression
+ + P-> len: Increase the len value. The implicit parenthesis is ++ (p-> len)
(++ P)-> len: first execute the p plus 1 operation, and then obtain the len value.
(P ++)-> len: first obtain the len value, and then perform the Add 1 operation of p.
* P-> str: obtains the value of the object pointed to by the pointer str.
* P-> str ++: first take the value of the object pointed to by the pointer str, and then add str to 1
(* P-> str) ++: first take the value of the object pointed to by the pointer str, and then add the object value to 1.
* P ++-> str: first take the value of the object pointed to by the pointer str, and then add the p value to 1.

Author: "dreambegin CSDN"

Related Article

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.