当前位置:网站首页>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 .
边栏推荐
猜你喜欢

How the computer accesses the Internet (IV) LAN and server response

实习报告1——人脸三维重建方法

CDN (content delivery network) content distribution network from entry to practice

C语言中的字符与字符串库函数的使用以及模拟实现

Atcoder beginer 202 e - count descendants (heuristic merge on heavy chain split tree for offline query)

NATBypass 端口转发

ShardingSphere数据库读写分离

Mobile robot (IV) four axis aircraft

nc 端口转发

干货|值得收藏的三款子域名收集工具
随机推荐
Today, I met a 38K from Tencent, which let me see the ceiling of the foundation
List of stringutils and string methods
NATBypass 端口转发
Canvas from getting started to persuading friends to give up (graphic version)
分家后印象笔记过日子依然不好过,骚操作却不少
Atcoder Beginner 202 E - Count Descendants(离线查询 重链剖分树上启发式合并)
MySQL数据库的一个问题
[waiting for insurance] what does waiting for insurance rectification mean? What are the rectification contents?
portfwd 端口转发
Js实现继承的六种方式
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
C语言实现静态版本的通讯录
JS & TS learning summary
Gan Development Series II (pggan, Singan)
Pat class A - check in and check out
It's time to consider slimming down your app
Keyboard input operation
实习报告1——人脸三维重建方法
Is it safe for qiniu to open an account?
Number theory division block explanation example: 2021 Shaanxi Race C