Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the definition, declaration and alignment of struct in cUniverse +

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to realize struct definition, declaration and alignment in c/c++". In daily operation, I believe many people have doubts about how to realize struct definition, declaration and alignment in c/c++. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to realize struct definition, declaration and alignment in c/c++"! Next, please follow the small series to learn together!

I. Definition/declaration method

The first type: only structure name, no variable definition/declaration

struct MyStruct{int i;char a[10];double b;};

The second type: having a structure name and declaring a variable name

struct MyStruct{int i;char a[10];double b;}structName;

or

struct MyStruct{int i;char a[10];double b;};

struct MyStruct structName;

//can be defined at the same time, such as struct MyStructName ={7,"xxxx", 2.1};

//can also be assigned directly between structs, such as struct MyStruct structName = structName1;

//The above is c style, struct MyStructName in c++ can omit struct or not.

The third type: no structure name, directly declare variables (for this structure, only one variable needs to be declared)

struct{int i;char a[10];double b;}structName;

The fourth type: with typedef

typedef struct MyStruct{int i;char a[10];double b;}structName;

structName=struct MyStruct, structName is an alias for the struct type, not a variable.

structName aa=struct MyStruct aa;

It can also be like this:

typedef struct{int i;char a[10];double b;}structName;

You can directly structName aa, the effect is the same as above.

typedef is mainly to save trouble, for c language definition structure variables always with struct keyword, typedef after not, and c++ itself

The struct keyword is not required, so typedef is not required.

II. Alignment

For example:

struct MyStruct{double dda1;char dda;int type;};int i = sizeof(MyStruct);

After vs2008 test i=16,"sizeof(MyStruct)=sizeof(double)+sizeof(char)+sizeof(int)=13" is incorrect. This is VC variable storage of a special process, in order to improve the CPU storage speed, VC on the start address of some variables do "alignment" processing. By default, VC specifies that the starting address of each member variable must be offset from the starting address of the structure by a multiple of the number of bytes occupied by the variable's type.

For the above example, 16=8+1+3+4, which is exactly a multiple of the number of byte boundaries of the structure (i.e., the number of bytes occupied by the type occupying the largest space in the structure sizeof(double)=8), so there are no empty bytes to fill.

So the size of the whole structure is: sizeof(MyStruct)=8+1+3+4=16, where 3 bytes are automatically filled by VC and nothing meaningful is put.

Another example:

structMyStruct{char dda;double dda1;int type;};

sizeof(MyStruct) is 24=1+7+8+4+4; the 11 bytes are automatically padded by vc, and the final 4 is a multiple of the number of bytes occupied by the type occupying the largest space in the structure in order to make the byte boundary of the structure.

At this point, the study of "struct definition, declaration and alignment in c/c++" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report