当前位置:网站首页>C language custom type explanation - Consortium
C language custom type explanation - Consortium
2022-07-24 17:39:00 【eat_ sleep_ play( )】
Structure :http://t.csdn.cn/fs4UT
enumeration :http://t.csdn.cn/GNP51
Catalog
2. Characteristics of the consortium
4. Calculation of the size of the consortium
Preface
Originality is not easy. , Please also give me three points of support , Your little three company has a great effect on me , Will spur us to forge ahead .
union ( Shared body )
1. Definition of joint type
Federation is also a special custom type
Variables defined by this type also contain a series of members , The feature is that these members share the same space ( So union is also called community )
example 1:
// Declaration of union type
union Un
{
char c;
int i;
};
int main()
{
// The definition of joint variables
union Un un;
// Calculate the size of the variables
printf("%d\n", sizeof(un));
return 0;
}
Code results :

2. Characteristics of the consortium
Members of the union share the same memory space , The size of such a joint variable , At least the size of the largest member ( Because the Union has to be able to keep at least the largest member ).
example 2:
// Declaration of union type
union Un
{
char c;
int i;
};
int main()
{
// The definition of joint variables
union Un u;
// Calculate the size of the variables
printf("%d\n", sizeof(u));
printf("%p\n", &u);
printf("%p\n", &(u.c));
printf("%p\n", &(u.i));
return 0;
}Code results :


At least on the first byte, everyone overlaps , Not all members of the consortium have their own separate space , But share the same space with everyone , So it's called community . It's like sharing , Have your own separate room , There are also rooms shared with others .
3. Problem practice
// Method 1
#include<stdio.h>
int Cheack_sys()
{
int a = 1;
return *(char*)&a;
}
int main()
{
int ret = Cheack_sys();
if (1 == ret)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}// Method 2 - Consortium solution
#include<stdio.h>
int Cheack_sys()
{
union un
{
int i;
char c;
}u;
u.i = 1;
return u.c;
}
int main()
{
int ret = Cheack_sys();
if (1 == ret)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}
Code results :


to i The assignment is 1, There is a total of 4 Bytes ,c and i Share the first byte , return 1 c If it is 1 It's the small end , Return is 0 It's big end .
4. Calculation of the size of the consortium
1. The size of the union is at least the size of the largest member .
2. 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 .
The consortium also has memory alignment .
example 1:
union Un1
{
char c[5];//5// The alignment number is 1
int i;//4// The alignment number is 4
};
union Un2
{
short c[4];//8// The alignment number is 2
int i;//4// The alignment number is 4
};
int main()
{
printf("%d\n", sizeof(union Un1));
printf("%d\n", sizeof(union Un2));
return 0;
}Code results :

Structure in custom type , Bit segment , Enumerations and consortiums will be finished here , I got a lot of support .
边栏推荐
- opencv自带颜色操作
- OpenCV 图片旋转
- Is Shenwan Hongyuan securities' low commission account reliable, reliable and safe
- Tensorflow introductory tutorial (37) -- DC Vnet
- Detailed explanation of ansible automatic operation and maintenance (V) the setting and use of variables in ansible, the use of jinja2 template and the encryption control of ansible
- es(1)
- TCP protocol debugging tool tcpengine v1.3.0 tutorial
- Six ways for JS to implement inheritance
- Array double pointer - deliberate practice
- Class bytecode file
猜你喜欢

面会菜评论分析

Wrote a few small pieces of code, broke the system, and was blasted by the boss

Tensorflow introductory tutorial (40) -- acunet

portmap 端口转发

ansible自动化运维详解(五)ansible中变量的设定使用、JINJA2模板的使用以及ansible的加密控制

Opencv has its own color operation

Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries

Gan Development Series II (pggan, Singan)

ROC and AUC details of the recommended system

Common questions of testers during interview
随机推荐
C # print reports using fastreport.net
滚动条调整亮度和对比度
Preliminary understanding of redis
Three.js (7): local texture refresh
Natbypass port forwarding
NATBypass 端口转发
Pat class A - A + B format
C语言自定义类型讲解 — 联合体
Six ways for JS to implement inheritance
图像像素的逻辑操作
hcip第三天
通道的分离与合并
In the morning, Tencent took out 38K, which let me see the ceiling of the foundation
Opencv has its own color operation
20 -- validate palindrome string
opencv自带颜色操作
C语言中的字符与字符串库函数的使用以及模拟实现
Getaverse, a distant bridge to Web3
Array learning navigation
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection