C language interview questions that appear at the same time between Intel and Microsoft

Source: Internet
Author: User
C language interview questions that appear at the same time between Intel and Microsoft

# Pragma pack (8)

Struct S1 {
Short;
Long B;
};

Struct S2 {
Char C;
S1 D;
Long long E;
};

# Pragma pack ()

Question
1. sizeof (S2) =?
2. How many bytes are left behind a in S1 of S2 and then B?

  If you know the answer, please write it in the discussion. Below are some answers from some netizens for your reference:

Answer:
Memory layout is
1 *** 11 **
1111 ****
1111 1111

So the answer is 24 and 3.

The following is a test program. I will know it after a try. I use vc2005.

# Pragma pack (8)

Struct S1 {
Short A; // 2 bytes
Long B; // 4 bytes
};
Struct S2 {
Char C; // 1 byte
S1 D; // 8 bytes
Long long e; // 8 bytes
};
// 1 *** 11 **
// 1111 ****
// 1111 1111
//

// 00 01 02 03 04 05 06 07
// 00 01 02 03 04 05 06 07
// 00 01 02 03 04 05 06 07
//
# Pragma pack ()

Int main (INT argc, char * argv [])
{
S2;
Char * P = (char *) &;
For (INT I = 0; I <24; ++ I)
P [I] = (char) (I % 8 );
Printf ("% d/N", sizeof ());
Printf ("c = 0x % LX/N", A. C );
Printf ("D. A = 0x % x/N", A. D. );
Printf ("d. B = 0x % x/N", A. D. B );
Printf ("e = 0x % LLX/N", A. E );
Return 0;
}
Result:
24
C = 0x0
D. A = 0x504
D. B = 0x3020100
E = 0x706050403020100

  Answer and analysis of redleaves:

If the code is:
# Pragma pack (8)
Struct S1 {
Char;
Long B;
};
Struct S2 {
Char C;
Struct S1 D;
Long long E;
};
# Pragma pack ()
The sizeof (S2) result is 24.
An important condition for member alignment is that each member is aligned separately. That is, each member is aligned in its own way.
That is to say, although alignment by 8 bytes is specified above, not all Members are alignment by 8 bytes. the alignment rule is that each member has a smaller alignment according to its type alignment parameter (usually the size of this type) and the specified alignment parameter (8 bytes here. in addition, the length of the structure must be an integer multiple of all alignment parameters used.

In S1, member A is 1 byte, which is aligned by 1 byte by default, and the specified alignment parameter is 8. Among the two values, 1 and a are aligned by 1 byte. Member B is 4 bytes, the default value is 4-byte alignment. In this case, it is 4-byte alignment, so sizeof (S1) should be 8;
In S2, C is the same as a in S1. It is aligned by 1 byte, while D is a structure. It is 8 bytes. What is its alignment? For the structure, its default alignment is the largest of all its members using alignment parameters, and S1 is 4. therefore, member D is aligned in 4 bytes. the member e is 8 bytes, which is aligned by 8 bytes by default. It is the same as the specified one, so it is connected to the boundary of 8 bytes. At this time, 12 bytes are used, therefore, four bytes are added, and the member e is placed starting from 16th bytes. at this time, the length is 24 and can be divisible by 8 (member e is aligned by 8 bytes. in this way, a total of 24 bytes are used.
A B
Memory layout of S1: 11 **, 1111,
C s1.a s1. B d
Memory layout of S2: 1 ***, 11 ***, 1111, *** 11111111

There are three important points:
1. Each Member is aligned in its own way, and the length can be minimized.
2. The default alignment of complex types (such as structures) is the alignment of its longest member, so that the length can be minimized when the member is a complex type.
3. The alignment length must be an integer multiple of the largest alignment parameter in the member. This ensures that each item is bounded when processing arrays.

  Answers and analyses of xue23 (xue23:

A program checks the memory address of each variable to find out:
The location of each variable in the memory is
C *** AA **
Bbbb ****
Dddddddd
The test code is as follows:
S2 SS;
Cout <"SS. c =" <& SS <Endl;
Cout <"SS. d. A =" <& SS. d. A <Endl;
Cout <"SS. d. B =" <& (ss. d. B) < Cout <"SS. d =" <& SS. e <Endl;
Print out the memory address of each variable.

So the answer is 24, 2.

However, in my imagination, the distribution should be as follows:
C *******
AA ** BBBB
Dddddddd

I wonder why C and A are put together to form an 8-bit length.

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.