当前位置:网站首页>Demonstration of C language structure function research
Demonstration of C language structure function research
2022-06-24 03:51:00 【Good pie notes】
C What are the functions of structures in language
What is the stress of structure member variable memory alignment ( a key )
A description of some concepts , I will not put C The definitions in language textbooks are brought up . Let's sit down and talk slowly .
=============================================================================================
What is the function of the structure
Three months ago , A senior student in the teaching and Research Office encountered this problem in the interview of Huawei Nanjing Research Institute . Of course , This is just the most basic question in the interview . If I ask you, how do you answer ?
This is my understanding ,C Structure in language has at least the following three functions :
(1) The properties of objects are organized organically .
such as , stay STM32 Of RTC In development , We need data to represent the date and time , These data are usually years 、 month 、 Japan 、 when 、 branch 、 second . If we don't use structures , Then we need to define 6 A variable to represent . In this case, the data structure of the program is loose , Our best data structure is “ High cohesion , Low coupling ” Of . therefore , It's better to use a structure to represent , Whether from the readability, portability or maintainability of the program :
typedef struct // Gregorian date and time structure
{
vu16 year;
vu8 month;
vu8 date;
vu8 hour;
vu8 min;
vu8 sec;
}_calendar_obj;
_calendar_obj calendar; // Define structure variables
(2) The function is replaced by the method of modifying the structure member variables ( Entrance parameters ) Redefinition of .
If the structure organically organizes the attributes of the object, it represents the structure “ see ”, Then replace the function by modifying the member variables of the structure ( Entrance parameters ) The redefinition of represents the structure “ of use ”. Continue to take the above structure as an example , Let's analyze . If now I have the following function to display the date and time :
void DsipDateTime( _calendar_obj DateTimeVal)
Then we just need to put one _calendar_obj Variables of this structure type are called as arguments DsipDateTime() that will do ,DsipDateTime() adopt DateTimeVal Variable to realize the display of content . If you don't use structures , We probably need to write such a function :
void DsipDateTime( vu16 year,vu8 month,vu8 date,vu8 hour,vu8 min,vu8 sec)
Obviously, such formal parameters are not considerable , Data structure management is also very cumbersome . If the return value of a function is a data representing the date and time , That's more complicated . It's just one thing .
On the other hand , If the user needs to indicate the date and time, the data should also include the week ( Zhou ), This is the time , If the mechanism has not been used before , Then it should be in DsipDateTime() Add a formal parameter to the function vu8 week:
void DsipDateTime( vu16 year,vu8 month,vu8 date,vu8 week,vu8 hour,vu8 min,vu8 sec)
It can be seen that this method to pass parameters is very cumbersome . So one of the advantages of using a structure as an entry parameter to a function is
Declaration of functions void DsipDateTime( _calendar_obj DateTimeVal) There is no need to change , Just add the member variables of the structure , Then, on the internal implementation of the function calendar.week Make corresponding treatment . such , In the modification of the program 、 It plays a significant role in maintenance .
typedef struct // Gregorian date and time structure
{
vu16 year;
vu8 month;
vu8 date;
vu8 week;
vu8 hour;
vu8 min;
vu8 sec;
}_calendar_obj;
_calendar_obj calendar; // Define structure variables
(3) The memory alignment principle of structure can improve CPU Access speed to memory ( Trade space for time ).
also , The address of the structure member variable can be based on the base address ( With offset offset) Calculation . Let's take a look at the following simple program , The analysis of this program will be in Section 2 Some structure member variables are described in detail in memory alignment .
#include<stdio.h>
int main()
{
struct // Declared structure char_short_long
{
char c;
short s;
long l;
}char_short_long;
struct // Declared structure long_short_cha
{
long l;
short s;
char c;
}long_short_char;
struct // Declared structure char_long_short
{
char c;
long l;
short s;
}char_long_short;
printf(" \n");
printf(" Size of char = %d bytes\n",sizeof(char));
printf(" Size of shrot = %d bytes\n",sizeof(short));
printf(" Size of long = %d bytes\n",sizeof(long));
printf(" \n"); //char_short_long
printf(" Size of char_short_long = %d bytes\n",sizeof(char_short_long));
printf(" Addr of char_short_long.c = 0x%p (10 Base number :%d)\n",&char_short_long.c,&char_short_long.c);
printf(" Addr of char_short_long.s = 0x%p (10 Base number :%d)\n",&char_short_long.s,&char_short_long.s);
printf(" Addr of char_short_long.l = 0x%p (10 Base number :%d)\n",&char_short_long.l,&char_short_long.l);
printf(" \n");
printf(" \n"); //long_short_cha
printf(" Size of long_short_char = %d bytes\n",sizeof(long_short_char));
printf(" Addr of long_short_char.l = 0x%p (10 Base number :%d)\n",&long_short_char.l,&long_short_char.l);
printf(" Addr of long_short_char.s = 0x%p (10 Base number :%d)\n",&long_short_char.s,&long_short_char.s);
printf(" Addr of long_short_char.c = 0x%p (10 Base number :%d)\n",&long_short_char.c,&long_short_char.c);
printf(" \n");
printf(" \n"); //char_long_short
printf(" Size of char_long_short = %d bytes\n",sizeof(char_long_short));
printf(" Addr of char_long_short.c = 0x%p (10 Base number :%d)\n",&char_long_short.c,&char_long_short.c);
printf(" Addr of char_long_short.l = 0x%p (10 Base number :%d)\n",&char_long_short.l,&char_long_short.l);
printf(" Addr of char_long_short.s = 0x%p (10 Base number :%d)\n",&char_long_short.s,&char_long_short.s);
printf(" \n");
return 0;
short s;
long l;
}char_short_long;
struct // Declared structure long_short_cha
{
long l;
short s;
char c;
}long_short_char;
struct // Declared structure char_long_short
{
char c;
long l;
short s;
}char_long_short;
printf(" \n");
printf(" Size of char = %d bytes\n",sizeof(char));
printf(" Size of shrot = %d bytes\n",sizeof(short));
printf(" Size of long = %d bytes\n",sizeof(long));
printf(" \n"); //char_short_long
printf(" Size of char_short_long = %d bytes\n",sizeof(char_short_long));
printf(" Addr of char_short_long.c = 0x%p (10 Base number :%d)\n",&char_short_long.c,&char_short_long.c);
printf(" Addr of char_short_long.s = 0x%p (10 Base number :%d)\n",&char_short_long.s,&char_short_long.s);
printf(" Addr of char_short_long.l = 0x%p (10 Base number :%d)\n",&char_short_long.l,&char_short_long.l);
printf(" \n");
printf(" \n"); //long_short_cha
printf(" Size of long_short_char = %d bytes\n",sizeof(long_short_char));
printf(" Addr of long_short_char.l = 0x%p (10 Base number :%d)\n",&long_short_char.l,&long_short_char.l);
printf(" Addr of long_short_char.s = 0x%p (10 Base number :%d)\n",&long_short_char.s,&long_short_char.s);
printf(" Addr of long_short_char.c = 0x%p (10 Base number :%d)\n",&long_short_char.c,&long_short_char.c);
printf(" \n");
printf(" \n"); //char_long_short
printf(" Size of char_long_short = %d bytes\n",sizeof(char_long_short));
printf(" Addr of char_long_short.c = 0x%p (10 Base number :%d)\n",&char_long_short.c,&char_long_short.c);
printf(" Addr of char_long_short.l = 0x%p (10 Base number :%d)\n",&char_long_short.l,&char_long_short.l);
printf(" Addr of char_long_short.s = 0x%p (10 Base number :%d)\n",&char_long_short.s,&char_long_short.s);
printf(" \n");
return 0;
therefore , The placement order of structure member variables affects the memory space occupied by the structure . The memory occupied by a structure variable is not necessarily equal to the sum of the space occupied by its member variables . If a user program or operating system ( such as uC/OS-II) When there are a large number of structural variables in , This memory footprint must be optimized , in other words , The arrangement order of member variables inside the structure is particular .
How are structure member variables stored ?
ad locum , I'll stop selling , The following conclusions are given directly , In the absence of #pragma pack In the case of macro :
principle 1 structure (struct Or in association with union) Data members of , The first data member is placed in offset by 0 The place of , In the future, the starting position of each data member storage should start from an integer multiple of the size of the member ( such as int stay 32 The location is 4 byte , From 4 An integer multiple of the address begins to be stored ).
principle 2 The total size of the structure , That is to say sizeof Result , Must be an integral multiple of the largest member inside , Make up for what is not enough .
* principle 3 When a structure is a member , Structure members should be stored from an address that is an integer multiple of the maximum element size inside .(struct a There is something inside struct b,b Are there in char,int,double When waiting for elements , that b It should be from 8 Start storing... At an integer multiple address of , because sizeof(double) = 8 bytes)
here , We combine the above program to analyze ( Don't discuss the principle for the time being 3).
Have a look first char_short_long and long_short_char These two structures , It can be seen from the addresses of their member variables , These two structures conform to the principle 1 And principles 2. Be careful , stay char_short_long In the address of the member variable of , char_short_long.s The address is 1244994, in other words ,1244993 yes “ Empty ”, Just being “ placeholder ” 了 !
边栏推荐
- Received status code 502 from server: Bad Gateway
- Typera cooperates with picgo to upload pictures to its own server with one click and obtain external links at the same time
- Rasa 3. X learning series -rasa 3.2.0 new release
- Tell you about mvcc
- 13. Tencent cloud IOT device side learning - data template function and Implementation
- Pine Script脚本常用内容
- Technical dry goods - how to use AI technology to accurately identify mining Trojans
- There is such a shortcut to learn a programming language systematically
- How to use elastic scaling in cloud computing? What are the functions?
- Build a small program + management background in 7 days, and this goose factory HR is blessed!
猜你喜欢

Flutter series: offstage in flutter

On game safety (I)

halcon知识:区域(Region)上的轮廓算子(2)

Installation of pytorch in pycharm

Halcon knowledge: contour operator on region (2)

元气森林推“有矿”,农夫山泉们跟着“卷”?

Ar 3D map technology

Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

黑帽SEO实战之通用301权重pr劫持
随机推荐
3D visualization of Metro makes everything under control
[numpy] numpy's judgment on Nan value
[congratulations] rock solid! A new generation of AMD Blackstone architecture instance is launched!
hprofStringCache
take the crown! Tencent security won the 2021 national network security week outstanding innovation achievement award
Web penetration test - 5. Brute force cracking vulnerability - (3) FTP password cracking
老弹出explorer.exe遇到问题已停止工作,怎么办?
Modstartcms theme introductory development tutorial
Difference and efficiency between get winevent and get eventlog
Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design
13. Tencent cloud IOT device side learning - data template function and Implementation
Dialogue with Google technical experts: soundstream is expected to be used for general audio coding in the future
Idea 1 of SQL injection bypassing the security dog
Troubleshooting and resolution of errors in easycvr calling batch deletion interface
Black hat SEO actual combat directory wheel chain generates millions of pages in batch
高斯光束及其MATLAB仿真
Web penetration test - 5. Brute force cracking vulnerability - (1) SSH password cracking
LeetCode 2006. Number of pairs whose absolute value of difference is k
web渗透测试----5、暴力破解漏洞--(4)Telnet密码破解
In the post epidemic era, "cloud live broadcast" saves "cloud cultural tourism"?