A detailed explanation of union

Source: Internet
Author: User
 

1. Overview

Union is defined in the same way as struct, but there is a fundamental difference between them.

Each member has its own memory space in the structure. The total length of a structure variable is the sum of the member lengths. In "Union", each member shares a piece of memory space. The length of a federated variable is equal to the longest length of each member.

2. Union Length

The original statement about Union memory allocation in the C programming language is:

1) A consortium is a structure.

2) The offset of all members of the consortium to the base address is 0.

3) The size of the structure space must be as large as the total capacity to accommodate the "widest" member.

4) In addition, the method should be suitable for all types of members in the consortium.

My understanding can be summarized as follows:

1) The structure space of a consortium must be large enough to be equal to the space of the longest structural variable. However, the longest space must meet the following conditions:

1. the space of a structure variable must be greater than or equal to the longest

2. in addition, the Data Length of other structural variables must be divisible, that is, the metadata type of the consortium space to other Members must be able to be divisible (int A [5], whose metadata type is int, the length of the element type is 4), which is actually the minimum public multiple of the element type.

For example

union   { float   fuel_load; char a[5];int   pallets; }fighter; 

In this struct, the space of each structure variable is float fuel_load, which occupies 4 bytes, char a [5], 5 bytes, int pallets, and 4 bytes. Through the statement "3) the structure space must be as large as the total capacity to accommodate the" widest "member", we can think that the space of the struct is five bytes, however, "the method is suitable for all types of members in the Consortium" is not satisfied. For this problem, it can be solved through the red font section above ., Therefore, the Consortium space is 8.8 and can be divided into 4 (float, int length) and 1 (char length), and 8 is greater than the array 5.

Another example is helpful to your understanding.

struct   aircraft { int   wingspan; int   passengers; union   { float   fuel_load; float   bomb_load; int   pallets; }; }fighter; 

 

Sizeof (fighter) is 12. Int wingspan; int passengers; two int-type 8 bytes. In union, all three are 4 bytes, so the Union length is 4 bytes.

3. Memory Allocation

In a word, all members of a consortium variable are public from low bytes. That is, all Members start from the lower byte.

We first allocate a space for the entire Union, which is described in the memory allocation in (2) above.

union {      int i;      char x[2];}a;int main(void){   a.x[0] = 10;   a.x[1] = 1;   printf("%d\n",a.i);   return 0;}

Shows its memory. A. X [0] is in low byte, and X [1] is in high byte. When the member variable I is called, its starting address is still from the starting address, which is output in four bytes. Therefore, it is 256 + 10 = 266

Program attachment:

#include <string.h>#include <stdio.h>#include <unistd.h>#if 0typedef union data{     float a;     float b;     int c;}data_t;int main(int argc ,char **argv){     printf("sizeof(float):%d\nsizeof(data_t):%d\n",sizeof(float),sizeof(data_t));     return 0;}#endiftypedef union data{     int i;     char x[2];}data_t;int main(int argc ,char **argv){        data_t datab;     memset(&datab,0,sizeof(datab));     datab.x[0] = 10;     datab.x[1] = 1;            printf("sizeof(float):%d\nsizeof(int):%d\n",sizeof(float),sizeof(int));     printf("datab.i :%d\n",datab.i);     return 0;}

4. The length of each data type in the Appendix

Type bytes

Int 4

Char 1

Short int 2

Bool 1

Long 4

Long long 8

Float 4

Double 8

Long Double 8

 

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.