1.Two ways to intialize 

2.anonymous struct

it can normally find and initialize.Beside it all the place can find and use this anomymous struct.

Even if two struct content are the same.

Complier will not find they are same.The reason i think is a struct type need to have a name that complier can find it but if they are not ,complier will not capable to find their inside content.But when a struct type element created,the all members have their memory and address to help find their.

3.the struct self-reference

struct a

{

int b;

struct a;
}

This a error manner.

struct a 

{

int b;

struct*a;

}

Using pointer to indirect is ok.

Anonymous struct can self-reference.

4.typedef used in struct

5.the phenomenon for struct memory alignment

(1) the struct first member will alin to the address which offset 0 for starting place.

(Offset--the gap byte number between the struct variable strating place and the starting place is viewed as 0)

(2)Other number all will have a alignment number,which the smallest value between struct member type size and complier default alignment value(vs 2022 is 8)

(3)Nested struct is using it member the biggest aalignment value.

(4)Array is using it element type.

6.Why it have struct alignment phenoment

(1) Platform migration question.Having some hardwares just can through 1 or 4 or 8 type to check value,which need struct variable must 4 or 8 multiple.

(2)Performance question

7.#pragma pack change default alignment

8.struct delivering parameter

Usually delivering address to save memory.

9.bit field

Pointing a struct member real memory occupy size,which unit is bit,it is used to save memory.

Attention,

first,bit field will ahead of time to create a char or int variable size to save value,whcih mean that bit field only can use for the size =< int size type.

second,bit field can not have too much capable to cross-platform because the bit field whether is singed or unsinged is not sure and the variable store direction is unsure.

Third,we can not use & for bit field member because bit is not address,which mean that we can not use scanf for it,only just can create a mid variable to assign to the bit field.

In vs 2022 situation example,

Some practice

1,

2,

From parctice2 and practice3,we can see concentrate the small value can significantly reduce the waste of memory.

  

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐