Explore the storage methods of struct & union

Source: Internet
Author: User

/*
* Test_struct.c
*
* Created on: 2009-10-3
* Author: lengyuex
*/

# Include
# Include
Int main (void)
{
Struct str_1 {
Char;
Int B;
} Good1;
Struct str_2 {
Int;
Char B;
} Good2;
Struct str_3 {
Int;
Char B;
Int c;
Char d;

} Good3;
Struct str_4 {
Int;
Int B;
Char c;
Char d;

} Good4;
Struct str_5 {
Char;
Char B;
Int c;
Int d;
} Good5;
Struct str_6 {
Char;
Char B;
Int c;
Int d;
Double e;
Double f;
} Good6;
Union uni_1 {
Char;
Int B;
} Good11;
Union uni_2 {
Int;
Char B;
} Good22;
Union uni_3 {
Int;
Char B;
Int C;
Char D;

} Good33;
Union uni_4 {
Int;
Int B;
Char C;
Char D;

} Good44;
Union uni_5 {
Char;
Char B;
Int C;
Int D;
} Good55;
Union uni_6 {
Char;
Char B;
Int C;
Int D;
Double E;
Double F;
} Good66;
Printf ("good1: % d \ ngood2: % d \ ngood3: % d \ ngood4: % d \ ngood5: % d \ ngood6: % d \ ngood11: % d \ ngood22: % d \ ngood33: % d \ ngood44: % d \ ngood55: % d \ ngood66: % d \ n ", sizeof (good1), sizeof (good2 ), sizeof (good3), sizeof (good4), sizeof (good5), sizeof (good6), sizeof (good11), sizeof (good22), sizeof (good33), sizeof (good44 ), sizeof (good55), sizeof (good66 ));
Printf ("char: % d \ n", sizeof (char ));
Printf ("int: % d \ n", sizeof (int ));
Printf ("short: % d \ n", sizeof (short ));
Printf ("long: % d \ n", sizeof (long ));
Printf ("float: % d \ n", sizeof (float ));
Printf ("double: % d \ n", sizeof (double ));
Printf ("a: 0x % x, B: 0x % x, c: 0x % x, d: 0x % x, e: 0x % x, f: 0x % x \ n ", (unsigned int) & good6.a, (unsigned int) & good6. B, (unsigned int) & good6.c, (unsigned int) & good6.d, (unsigned int) & good6.e, (unsigned int) & good6.f );
Exit (0 );
}

The output is as follows: [lengyuex @ Tux algorithm] $./a. out
Good1: 8
Good2: 8
Good3: 16
Good4: 12
Good5: 12
Good6: 28
Good11: 4
Good22: 4
Good33: 4
Good44: 4
Good55: 4
Good66: 8
Char: 1
Int: 4
Short: 2
Long: 4
Float: 4
Double: 8
A: 0xbff7cc9c, B: 0xbff7cc9d, c: 0xbff7cca0, d: 0xbff7cca4, e: 0xbff7cca8, f: 0xbff7ccb0

In the case of 6th, I felt a little wrong. So I used the address to verify it later. I found that even if there is a double, it is also 4-byte aligned.

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.