当前位置:网站首页>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 ” 了 !

原网站

版权声明
本文为[Good pie notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210917190323858X.html