当前位置:网站首页>Custom structure type
Custom structure type
2022-06-25 14:58:00 【--988】
Catalog
Bit segment
The declaration and structure of a bit segment are similar , There are two differences :
1. Member of bit segment Must be int,unsigned int or signed int.
2. The member name of the segment is followed by a colon and a number ( Usually of the same type , There can be char)
Bit segment memory allocation , Save a space
1 The members of a segment can be int Or is it char( It belongs to the integer family ) type
2. The space of the bit segment is based on 4 Bytes (int) Or a byte (char) The way to open up .
3. Bit segments involve many uncertainties , Bit segments are not cross platform , Focus on portable programs , Bit segments should be avoided .
Bit segment : Binary digit 1Byte=8bit
struct S
{
int_a:2;//2 Binary bits , Two bit
int_b:5;
int_c:10;
int_d:30;
};
int main()
{
struct S s;
printf("%d\n",sizeof(s));//8 Bytes
return 0;
}
The cross platform problem of bit segment :
- int It's uncertain whether a bit segment is treated as a signed number or an unsigned number .
- The number of the largest bits in the bit segment cannot be determined (16 The machine is the largest 16,32 The machine is the largest 32, It's written in 27, stay 16 A bit of the machine will go wrong ).
- The members in the bit segment are allocated from left to right in memory , Or right to left allocation criteria have not yet been defined .
4. When a structure contains two bit segments , The second segment is relatively large , Cannot hold the remaining bits of the first bit segment , Whether to discard the remaining bits or to use , This is uncertain .
enumeration
enum Sex
{
MALE, //MALE=0, Enumeration defaults from 0 Start , Increase in turn 1
FEMALE, // Possible values of enumeration : Constant
SECRET //MALE=2, You can also assign an initial value to a constant , Then it cannot be changed
};
int main()
{
enum Sex s=MALE;// Assign an enumeration constant to an enumeration variable , There will be no type difference
s=FEMALE; // Only these three elements can be assigned , Cannot assign to a class other than enumeration
return 0;
}
enum Color
{
RED, //0
GREEN, //9 If given GREEN Assign initial value to 9
BLUE //10
};
#define RED 0
int mian()
{
int color=0;// This sentence is wrong , Enumeration type !=int type
printf("%d %d %d\n",RED,GREEN,BLUE);
return 0;
}Advantages of enumeration : It's big and small 4 Bytes
have access to #define Define constants , Why use enumeration ?
- Increase the readability and maintainability of the code
- and #define The defined identifier comparison enumeration has type checking , More rigorous
- Prevent named pollution
- Easy to debug
- Easy to use , You can define more than one constant at a time
union
Characteristics of Union :
Members of the union share the same memory space , The size of such a joint variable is at least the size of the largest member ( Because the Union has to be able to keep at least the largest member ) Members cannot be used at the same time .
check-sys()
{
int a=1;
return *(char*)&a;// return 1, Represents the small end ; return 0, It means big end .
}
int check-sys()
{
union Un
{
char c;
int i;
}u;
u.i=1;
return u.c;
}
int main()
{
int a=1;
int ret=check-sys();
if(1==*(char*)&a)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}Calculation of joint size :
The size of the union is at least the size of the largest member , When the maximum member size is not an integral multiple of the maximum number of alignments , It's about aligning to an integer multiple of the maximum number of alignments .
union Un
{
int a;
char arr[5]; // The alignment number is 1
};
int main()
{
union Un u;
printf("%d\n",sizeof(u)); // waste 3 Bytes , The maximum number of alignments is 8
}边栏推荐
- QQ love talk candy love talk content acquisition and storage
- Basic usage of markdown (plain text and grammar)
- Iterator failure condition
- 【中国海洋大学】考研初试复试资料分享
- Jaspersoft studio installation
- C language LNK2019 unresolved external symbols_ Main error
- Daily question, magic square simulation
- JS floating point multiplication and division method can not accurately calculate the problem
- Why should the coroutine be set to non blocking IO
- QT opens the print dialog box in a text editor
猜你喜欢

About the problem of kicad stuck in win10 version, version 6 x

How to cut the size of a moving picture? Try this online photo cropping tool

Application of TSDB in civil aircraft industry

‘make_ unique’ is not a member of ‘std’

电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备

QT loading third-party library basic operation

Add the resources directory under test in idea

从0到1完全掌握 XSS

Position (5 ways)

90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟
随机推荐
Two advanced playing methods of QT signal and slot
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
QQ love talk candy love talk content acquisition and storage
使用sphinx根据py源文件自动生成API文档
NBD Network Block Device
Basic knowledge of pointer
basic_ String mind map
Kubernetes understands kubectl/ debugging
Js- get the mouse coordinates and follow them
High precision addition
Arithmetic operations and expressions
How to combine multiple motion graphs into a GIF? Generate GIF animation pictures in three steps
Std:: vector minutes
[HBZ sharing] use of locksupport
电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
Native JS obtains form data and highlights and beautifies JSON output display
What moment makes you think there is a bug in the world?
Compile Caffe's project using cmake
挖财是正规的吗?股票开户安全吗?
全国首例,中国电信 5G 井下人员定位项目正式商用:可实时跟踪位置,保障作业安全