Struct, Consortium, memory allocation

Source: Internet
Author: User
# Include <stdio. h> struct a {char X [6]; int y; char Z [6] ;}; struct B {int y; char X [6]; char Z [6] ;}; Union c {char X [6]; int y; char Z [6] ;}; int main () {printf ("% d, % d, % d \ n ", sizeof (struct a), sizeof (struct B), sizeof (Union C); Return 0 ;}

Running result: 20, 16, 8

Each element in the struct occupies the memory space independently, and the total space is the sum of the space occupied by each element. Although the elements of A and B are the same, the order is different and the memory space occupied by the 4-byte alignment is different.

Each element in the consortium shares a piece of memory space. Only one element can be used at a time. The total space is the space occupied by the largest element (8 in the answer is because 4-byte alignment is taken into account ).

Struct, Consortium, memory allocation

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.