"C Language learning" union keyword

Source: Internet
Author: User

The Union keyword is very similar to the use of the struct keyword, first look at the example:

#include <stdio.h>int main (void) {/*struct struct */struct{int i;char a[4];} S;/*union structural body */union{   int i;   Char a[4];} u;printf ("sizeof (int) =%d\n", sizeof (int)),/*s struct assignment */s.a[0] = 0x00;s.a[1] = 0x01;s.a[2] = 0x00;s.a[3] = 0x00;printf ("s.i =%d\n ", s.i);/*u structure Assignment */u.a[0] = 0x00;u.a[1] = 0x01;u.a[2] = 0x00;u.a[3] = 0x00;printf (" u.i=%d\n ", u.i);}

Printing results:

sizeof (int) =4

s.i=-858993460

u.i=256

In the above example, the s struct occupies 8 bytes of memory structure:


The memory structure assigned to the S struct is:


The memory structure occupied by the U structure is 4 bytes:


The memory structure after the U structure is assigned is:


Structure, the difference between a struct and a union is the allocation of memory. The memory allocation of a struct is the sum of the member memory, such as the S struct, which accounts for int+char[4]=4+4=8 bytes. The Union only configures a large enough space to accommodate the maximum length of the data member, that is, the union's largest memory-occupying member is the maximum RAM for the union structure. In other words, the union's memory is common to all members, such as the example above, where the member variables of the U struct have int and char arrays, with lengths of 4 and 4 respectively. You use memory to go to the maximum memory value of a member variable, so it is still 4 bytes. The access to the union member is starting at 0 from the base address of the consortium, and when assigning a value to the char array of u, it is allocated from the byte-low (base address) of U. After assignment, the value of I is obtained from the union u when printing u.i, and according to the Union rules, the memory is public and the offset from the base address of the consortium is 0 to get 4 bytes at the beginning. The memory structure after U-Assignment is equivalent to 256 when integers are processed. The S.I is not assigned and can only get random numbers.

Summary: The benefit of Union is that it saves memory space, but it has to be used for the occasion.

Note: The storage mode of the system directly affects the value of the union structure, such as the big-endian mode and the small-end mode.

"C Language learning" union keyword

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.