当前位置:网站首页>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 .
边栏推荐
- Niuke linked list solution record
- Socat port forwarding
- Six ways for JS to implement inheritance
- Supervisor common commands
- Logical operation of image pixels
- Scept: consistent and strategy based trajectory prediction for planned scenarios
- Portmap port forwarding
- Link editing tips of solo blog posts illegal links
- Huawei machine test - topic core test point
- The most powerful programmer on the earth is equipped with a "three piece set". Do you know what it is?
猜你喜欢

es(1)

Dry goods | three sub domain name collection tools worth collecting

Separation and merging of channels

Use Matplotlib to simulate linear regression

Iqiyi Tiktok reconciled, Weibo lying gun?

NPM install reported -4058 error

Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable

Getaverse,走向Web3的远方桥梁

Open source Invoicing system, 10 minutes to complete, it is recommended to collect!

键盘输入操作
随机推荐
MySQL数据库的一个问题
2022 Yangtze River Delta industrial automation exhibition will be held in Nanjing International Exhibition Center in October
C语言编程训练题目:左旋字符串中的k个字符、小乐乐与欧几里得、打印箭型图案、公务员面试、杨树矩阵
The most powerful programmer on the earth is equipped with a "three piece set". Do you know what it is?
还在用Xshell?你out了,推荐一个更现代的终端连接工具!
Atcoder beginer 202 e - count descendants (heuristic merge on heavy chain split tree for offline query)
Is computer monitoring true? Four experiments to find out
实习报告1——人脸三维重建方法
Js实现继承的六种方式
Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries
Can CSC open an account for domestic futures? Is it safe?
Array double pointer - deliberate practice
High performance complexity analysis of wechat circle of friends
Portfwd port forwarding
Are the top ten securities companies safe and risky to open accounts?
hcip第四天笔记
ShardingSphere数据库读写分离
C # print reports using fastreport.net
Trends of semiconductor industry
Wallys/3 × 3/2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz/QCN9074