In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to customize structures, bit segments and enumerations in c language, the content is detailed and easy to understand, the operation is simple and quick, and has a certain reference value. I believe you will gain something after reading this article on how to customize structures, bit segments and enumerations in c language. Let's take a look.
Structure type creation:
Struct Stu
{char name [20]; / / first name
Int age;// age
Char sex [5]; / / gender
Char id [20]; / / student number
} stu1,stu2
Self-reference of the structure:
Struct Node
{int data
Struct Node* next
}; / / pointer is used
Initialization of structure: (can be initialized as a whole, but cannot be assigned as a whole)
Struct Stu / / Type declaration
{char name [15]; / / first name
Int age; / / Age
}
Struct Stu s = {"zhangsan", 20}; / / initialize
(key) memory alignment of the structure:
First of all, we have to master the alignment rules of the structure: 1. The first member is at the address where the offset from the structure variable is 0. two。 Other member variables are aligned to the address of an integer multiple of a number (alignment).
The total size of the structure is an integral multiple of the maximum alignment (each member variable has an alignment). 4. If the structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment, and the overall size of the structure is an integer multiple of all the maximum alignment (including the alignment of the nested structure).
Why should memory be aligned:
Platform reason (migration reason): not all hardware platforms can access any data on any address; some hardware platforms can only fetch certain types of data at certain addresses, otherwise a hardware exception is thrown
Performance reasons: data structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access unaligned memory, the processor needs to make two memory accesses; aligned memory accesses require only one access.
(exchanging space for time)
Parameters passed by structure:
When the function passes parameters, the parameters need to be stacked, and there will be system overhead in time and space.
If you pass a structure object, the structure is too large, the parameter stack of the system overhead is relatively large, so it will lead to performance degradation.
Conclusion: when the structure passes parameters, it should pass the address of the structure.
Bit segment: (you can resize the structure)
1. The member of the segment must be int, unsigned int or signed int or char.
two。 The member name of the bit field is followed by a colon and a number.
Struct A
{int _ a 2; / / the number represents the number of bit bits occupied
Int _ b:5
Int _ c:10
Int _ d:30
}
Memory allocation of bit segments
The member of the bit segment can be int unsigned int signed int or char (belonging to the × × family) type 2. The space of the bit segment is opened up in the form of 4 bytes (int) or 1 byte (char) as needed. 3. Bit segments involve a lot of uncertainties, bit segments are not cross-platform, and portable programs should avoid using them.
Bit size:
. When a structure contains two bit segments and the second bit segment is too large to accommodate the remaining bits of the first bit segment, it is uncertain whether to discard the remaining bits or to use them. (if the space behind is enough to make up directly, if not, find another sufficient space) (return the first address of the new address)
Enumerate:
Enum Day// week
{Mon
Tues
Wed
Thur
Fri
Sat
Sun
}
These possible values are all valuable, starting with 0 by default and incrementing by 1 at a time. Of course, initial values can also be assigned at the time of definition. Only enumerated constants can be assigned to enumerated variables. It can be copied directly. The value increases intermittently.
Enumeration size:
Add it together
Joint:
A federation is also a special custom type in which variables defined by this type also contain a series of members characterized by the fact that these members share the same space (so federation is also called a common body).
Union Un2
{short c [7]
Int i
}
Union size:
The size of the union is at least the size of the largest member. When the maximum member size is not an integer multiple of the maximum alignment, it should be aligned to an integer multiple of the maximum alignment.
This is the end of the article on "how to customize structures, bits, and enumerations in C language". Thank you for reading! I believe you all have some knowledge of "how to customize structures, bits and enumerations in c language". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.