Analysis of C language structures and segments

  • 2020-06-12 10:08:11
  • OfStack

1. Definition of structure

In practice, data often exists in groups. If these values are of different types, they cannot be stored in the same array at the same time. In C, you can use a structure to store values of different types starting at 1, so the structure is also a collection of values called its members, but these members can be of different types.

Development:

A "structure" is a constructed data type, also known as a user-defined data type, that is composed of several "members." Each member can be a basic data type or another construct type. A structure is a "constructed" data type that must be defined before it can be described and used. Just as you define 1 before you describe and call a function.

2. Initialization of structure

The initialization of a structure is similar to that of an array, located within a pair of curly braces and separated by commas, and can be used to initialize individual members of the structure. These values are written out in order of the structure members, and if the initial values are insufficient, the remaining structure members are initialized with the default values.


struct S
{
  int a;
  int b[10];
  char c[20];
}s={
    10,
    {1,2,3,4,5},
    "abc"
  };

typedef: In C, define 1 struct type if typedef is used:


typedef struct Student
{
  int a;
}Stu;

When you need to declare a variable, you can simply Stu.stu1; The absence of typedef is declared by struct Student stu1. (Stu = = struct Student);

4. Memory storage of the structure

The structure is stored in memory to allocate memory to each member in the order of one member after another. While the allocation follows the alignment rules of the structure body:

(1). The first member is at the address where the offset of the structure variable is 0; (2). Other member variables should be aligned to the address of an integer multiple of the aligned number; Alignment number: the compiler's default alignment number is a smaller value than the member size; (Default alignment number vs: 8, linux: 4) (3). The total size of the structure is an integer multiple of the maximum alignment number. (4). In the case of nested structure, the nested structure is aligned to an integer multiple of its own maximum alignment number, and the overall size of the structure is an integer multiple of all maximum alignment Numbers.

struct S
{
  int a;
  char b;
  double c;
};
// The size of this structure is 16

stuct A
{
  doulbe d;
  char e;
};
struct S
{
  int a;
  char b;
  struct A C;
  double c;
};
// The structure of the body S The size of 32

Reasons for memory alignment:

(1) Platform reason: Not all hardware platforms can access arbitrary data at any address; Some hardware platforms can only fetch certain types of data at certain addresses or throw hardware exceptions. (2) performance reasons: Data structures should be aligned on natural boundaries as much as possible. The reason is that in order to access unaligned memory, the processor needs to make two memory accesses, while aligned memory accesses only need one.

5. Structural body segment

A bit segment has a similar declaration and structure, but its members are fields of one or more bits. A bit member must declare a bit int, unsigned int or signed int type. The member is followed by a colon and an integer, which is the size of the bit occupied by the bit segment.


struct S
{
  int a : 7;
  signed int b : 6;
  unsigned int c : 12;
};

Data storage of bit segment:

When a declaration specifies 2 bit segments, the 2 bit segment cannot be placed on the rest of the 1 bit segment, and the compiler may place the 2 bit segment on the next byte of memory, or directly after the 1 bit segment.

Bit segment size:

The size of the bit segment depends on the editor's data store for the bit segment:


struct S
{
  int a : 1;
};
//S The size of 4
struct S
{
  int a : 1;
  int b : 5;
  int c : 6;
};

conclusion


Related articles: