C/C++ custom type (1)

Homepage:

There are still unknowns waiting to be explored_Data structure, PTA, C language difficulties-CSDN Blog

Topic column---C language difficulties:

C Language Difficulties_There are still unknown blogs waiting to be explored-CSDN Blog

Table of contents

I. Introduction 

2. Structure type

1. What is a structure?

2. Format of structure

3. Definition of structure variables

4. Initialization of structure variables

5. Size of structure type 

6. Advantages of memory alignment

1. Platform reasons:

2. Performance reasons:

3. Overall:

7. Modify the default alignment number

3. Position

1. What is a bit segment?

 2. The format of the bit segment

3. Definition and initialization of bit segment variables 

4. The size of the bit segment

5. Memory allocation of bit segments


I. Introduction 

If you are asked to describe the price of a book, you can use built-in data types such as int or double.

What if you were asked to describe an entire book? It is not enough to describe it simply by price. In addition to price, books also have other attributes such as publisher, author, edition, etc.

It can be seen that just using built-in data types is not enough. Therefore, you need to define your own data type to describe the complex thing "book".

What are the custom types? What are the characteristics? Let's take a look next.

2. Structure type

1. What is a structure?

A structure (struct) is a data collection composed of a series of data of the same type or different types, also called a structure.

2. Format of structure

struct Book
{
    //书的各种属性
};

Note: 

1.struct is a keyword, which means that what follows is a structure.

2. Book is a label, it does not have to be Book (needs to be customized, optional). Can be omitted, if omitted the structure can only be used once.

3. There must be a semicolon after the final brace .

3. Definition of structure variables

If you want to describe a book, the definition is as follows:

 Structure variables can be defined where the two arrows point.

But if there is no flag word, it cannot be defined in the same way as s2 variable definition. An error will be reported as follows:

4. Initialization of structure variables

 It cannot be initialized directly in the structure.

 It needs to be initialized and assigned as shown in the picture above.

Warm reminder: The first method is only applicable to the writing method in the picture above. If you assign a value like the first method after creating the variable, an error will be reported. 

5. Size of structure type 

How much space does the structure type occupy? Is it simply a matter of adding up the space opened up by the built-in type variables?

You can copy the code to see if the space occupied is different from what you think.

#include<stdio.h>
#include<string.h>
struct s1
{
    char a;
    char b;
    int c;
};
struct s2
{
    char a;
    int c;
    char b;
};
int main()
{
    printf("%zd\n", sizeof(struct s1));
    printf("%zd\n", sizeof(struct s2));
    return 0;
}

The space occupied by the structure is not a simple addition. Instead, it requires memory alignment operations.

Memory alignment rules:

1. The first member is at the address offset 0 from the structure variable.
2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number).
    Alignment number = The smaller of the compiler's default alignment number and the size of the member.
    The default value in VS is 8
3. The total size of the structure is an integer multiple of the maximum alignment number (each member variable has an alignment number).
4. If a structure is nested and the nested structure is aligned to an integer multiple of its own maximum alignment number, the overall size of the structure is an integer of all maximum alignment numbers (including the alignment number of nested structures) times

for example: 

#include<stdio.h>
#include<string.h>
struct s
{
    char a;
    char b;
    int c;
};
int main()
{
    printf("%zd",sizeof(struct s));
    return 0;
}

6. Advantages of memory alignment

1. Platform reasons:


Not all hardware platforms can access any data at any address; some hardware platforms can only fetch certain types of data at certain addresses , otherwise a hardware exception will be thrown.

2. Performance reasons:

Data structures (especially stacks) should be aligned on natural boundaries whenever possible. The reason is that in order to access unaligned memory, the processor needs to make two memory accesses; aligned memory access requires only one access.

3. Overall:

Memory alignment of structures trades space for time.

7. Modify the default alignment number

The default alignment number can be modified through the #pragma pre-directive.

#include<stdio.h>
#pragma pack(4)//修改默认对齐数为4
struct s
{
    char a;
    char b;
    int c;
};
#pragma pack()//还原默认对齐数
#pragma pack(1)//修改默认对齐数为1
int main()
{
    printf("%d", sizeof(struct s));
    return 0;
}

3. Position

1. What is a bit segment?

The declaration of bit fields is similar to that of structures.

The difference between bit segments and structures is that structures save time and bit segments save space.

1. The members of the bit segment are generally int, unsigned int, signed int, and char.
2. There is a colon and a number after the member name of the bit field.

 2. The format of the bit segment

struct A
{
    int _a : 2;
    int _b : 5;
    int _c : 10;
};

Each member definition is followed by a colon and a number.

The meaning of the number is the space to be opened, and the unit is bit.

3. Definition and initialization of bit segment variables 

Operations are similar to structures.

4. The size of the bit segment

According to the above code, 2+5+10=17 bits, (1 byte has 8 bits), so the size of the bit field is 3 bytes. This is wrong. Although the purpose of bit segments is to save space, it does not completely waste space.

5. Memory allocation of bit segments

 1. Members of the bit field can be int unsigned int signed int or char (belonging to the integer family) type
2. The space of the bit field is 4 bytes (int) or 1 byte (char) as needed To open up.
3. Bit segments involve many uncertain factors. Bit segments are not cross-platform. Programs that focus on portability should avoid using bit segments.

Because they are all of char type, each byte is allocated 8 bits. If the remaining memory in one byte is not enough, another byte will be allocated for storage. As shown in the picture below, there will be data loss, which is normal.

 

What I described is consistent with what the compiler said, and there is no deviation in my understanding. 

thanks for your support! 

Guess you like

Origin blog.csdn.net/qq_73435980/article/details/133578153