当前位置:网站首页>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 ” 了 !
边栏推荐
- JVM调优简要思想及简单案例-怎么调优
- How to avoid man in the middle attack (mitm)
- 给你讲懂 MVCC
- A problem of testing security group access in windows10
- 内存泄漏之KOOM
- How to choose excellent server hosting or server leasing in Beijing
- Actual battle case | refuse information disclosure, Tencent cloud helps e-commerce fight against web crawlers
- The importance of the computer room to the stable operation of the server
- How to select the application of the server?
- [congratulations] rock solid! A new generation of AMD Blackstone architecture instance is launched!
猜你喜欢

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

SQL注入绕过安全狗思路一

Black hat SEO practice: General 301 weight PR hijacking

Common content of pine script script

Flutter series: offstage in flutter

Old popup explorer Exe has stopped working due to problems. What should I do?

ModStartCMS 主题入门开发教程

Idea 1 of SQL injection bypassing the security dog

浅谈游戏安全 (一)

Halcon knowledge: contour operator on region (2)
随机推荐
Differences between EDI and VMI
How the new operator works
How to choose excellent server hosting or server leasing in Beijing
How to avoid man in the middle attack (mitm)
Hprof information in koom shark with memory leak
On game safety (I)
[numpy] numpy's judgment on Nan value
Getlocationinwindow source code
Common content of pine script script
How to handle the uplink and downlink silence of TRTC
内存泄漏之KOOM-Shark中的Hprof信息
Some basic knowledge of data center server cabinet
开源一款监控数据采集器,啥都能监控
Koom of memory leak
ClickHouse(02)ClickHouse架构设计介绍概述与ClickHouse数据分片设计
Summary of rust high concurrency programming
Cross platform RDP protocol, RDP like protocol and non RDP protocol remote software
How does cloud computing achieve elastic scaling? What are the characteristics of elasticity?
Garbage collection mechanism
左滑从小窗到大窗口DispatchFrameLayout