Introduction to memory allocation principles in struct and linux

Source: Internet
Author: User
Talking about struct and the memory allocation principle in linux The struct is widely used in the program. The more complicated the problem to be handled, the more complicated the data volume is, the more necessary the struct is. Such as the network, kernel, and driver. Skillful use of struct is crucial to every programmer. A good conclusion... information &
Talking about struct and the memory allocation principle in linux The struct is widely used in the program. The more complicated the problem to be handled, the more complicated the data volume is, the more necessary the struct is. Such as the network, kernel, and driver. Skillful use of struct is crucial to every programmer. A good structure not only makes the program architecture clear, but also makes the operation flexible and scalable. The reason why the linux network is powerful is that it has a lot to do with the well-designed data structure. Www.2cto.com struct definition: [cpp] struct a {int xx: 4; int yy: 4;}; struct initialization: Method 1: [cpp] struct a aa = {xx: 2, yy: 3}; www.2cto.com Method 2: [cpp] struct a cc = {. xx = 6 ,. yy = 1 ,}; method 3: [cpp] struct a dd = {4, 2}; in the definition, you can restrict the bit scope of the variable, such as the preceding: int xx: 4; this indicates that xx's valid domain has only four digits, that is, the maximum value assigned to him is 15. if this value is exceeded, the compiler reports the following error: warning: overflow in implicit constant conversion. Here, if you assign a value of 15 to xx, for example, [cpp] struct a cc = {. xx = 15 ,. yy = 1,}; www.2cto.com and then output: [cpp] printf ("cc. xx = % d \ n ", cc. xx); the result will be:-1 because the xx defined here is int type, the binary 1111 of 15, the highest bit is 1, expressed as a negative number, therefore, the value after 1 is obtained is 0001. So it is-1. The advantage of this bit field operation is that when you do not need to use the length of your defined type, you can add a bit field operation to save memory space. The other questions raised here use sizeof (struct a) to get 4. if no bit field limit is added, it is 8. why is it 4? 4 bit + 4 bit should be exactly 1 byte. it should be 1. This is because I compiled and executed in linux, and in linux, the minimum value of memory allocation is half of the type value. (I did an experiment in linux) I have defined a struct as follows: [cpp] struct a {short int xx: 2; short int yy: 2;} bb; as shown above, I define two short int type values. The short int type is 2 bytes in 32-bit linux. here, the two values are less than one byte, but the output value of sizeof (struct a) is 2. The system automatically fills up less than one byte. [Html] struct a {short int xx: 9; short int yy: 2;} bb; the output of www.2cto.com is also 2. Xx is more than one byte, but 9 + 2 = 11 is not more than 16 (2 byte). I guess the system has allocated the 1bit xx exceeded to the 1bit yy. [Cpp] struct a {short int xx: 9; short int yy: 9;} bb; the output is 4. 9 + 9 = 18, over 16, the system allocates 2 bytes to xx and yy. Author: linuxblack125125

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.