Home > Software engineering >  Questions about structure byte alignment
Questions about structure byte alignment

Time:10-05

The recent task is completed, see again, pick up some basic books, make up for their basic knowledge, have a problem, ask people here,
See the byte alignment, write their own a few tests, the results surprise:
 
Struct test1 {
Char text [2];
Long number1.
Long long number2.
};
Int nsize=sizeof (test1);

Structure test1 is easy to understand, the size of the byte alignment according to the theory of the char, the system automatically fill 2 bytes, long size is 4, 8, is the size of the long long so test1 is 16, the size of the run results agree with my idea, but I don't understand the following
 
Struct test2 {
Char text [5].
Long number1.
Long long number2.
};
Int nsize=sizeof (test1);

 
Struct test3 {
Char text [2];
Long long number2.
Long number1.
};
Int nsize=sizeof (test1);

Structure the size of the test2 and test3 is 24, excuse me, why both structure size is 24?

CodePudding user response:

The element is the starting address sizeof (element type) integer times, structure size is the largest element integer times,
So
Test2
Text 5 bytes, empty 3 bytes
Number1 4 bytes, empty 4 bytes
Number2 8 bytes

Test3
Text 2 bytes, empty 6 bytes
Number2 8 bytes
Number1 4 bytes, empty 4 bytes

CodePudding user response:

reference 1st floor akirya response:
element is the starting address of the sizeof (element type) integer times, structure size is the largest element of integer times,
So
Test2
Text 5 bytes, empty 3 bytes
Number1 4 bytes, empty 4 bytes
Number2 8 bytes

Test3
Text 2 bytes, empty 6 bytes
Number2 8 bytes
Number1 4 bytes, empty 4 bytes

Test1 said that according to this, the structure should be 24, why is 16, just don't understand this

CodePudding user response:

refer to the second floor soundbird response:
Quote: refer to 1st floor akirya response:

The element is the starting address sizeof (element type) integer times, structure size is the largest element integer times,
So
Test2
Text 5 bytes, empty 3 bytes
Number1 4 bytes, empty 4 bytes
Number2 8 bytes

Test3
Text 2 bytes, empty 6 bytes
Number2 8 bytes
Number1 4 bytes, empty 4 bytes

Test1 said that according to this, the structure should be 24, why is 16, just don't understand this

Test1 is what you said,

CodePudding user response:

In test1 2 + 4=6 and again 2 bytes aligned to 8 bytes, so it is 8 + 8=16
Test2 5 + 4=9, 8 bytes, respectively aligned to only 8 bytes, so is 8 + 8 + 8=24

CodePudding user response:

Fyi:
 # include & lt; stdio.h> 
# define field_offset (s, f) (int) (& amp; (((struct s *) (0)) - & gt; F))
Struct AD {int a; Char b [13]. Double c; };
# pragma pack (push)
The # pragma pack (1)
Struct A1 {int a; Char b [13]. Double c; };
# pragma pack (2)
Struct A2 {int a; Char b [13]. Double c; };
# pragma pack (4)
Struct A4 {int a; Char b [13]. Double c; };
# pragma pack (8)
Struct A8 {int a; Char b [13]. Double c; };
# pragma pack (16)
Struct A16 {int a; Char b [13]. Double c; };
# pragma pack (pop)
Int main () {
Printf (" AD. A % d \ n ", field_offset (AD, a));
Printf (" AD. B % d \ n ", field_offset (AD, b));
Printf (" AD. C % d \ n ", field_offset (AD, c));
Printf (" AD sizeof % d \ n ", sizeof (AD));
printf("\n");
Printf (" A1. A % d \ n ", field_offset (A1, a));
Printf (" A1. B % d \ n ", field_offset (A1, b));
Printf (" A1. C % d \ n ", field_offset (A1, c));
Printf (" A1 sizeof % d \ n ", sizeof (A1));
printf("\n");
Printf (" A2. A % d \ n ", field_offset (A2, a));
Printf (" A2. B % d \ n ", field_offset (A2, b));
Printf (" A2. C % d \ n ", field_offset (A2, c));
Printf (" A2 sizeof % d \ n ", sizeof (A2));
printf("\n");
Printf (" A4. A % d \ n ", field_offset (A4, a));
Printf (" A4. B % d \ n ", field_offset (A4, b));
Printf (" A4. C % d \ n ", field_offset (A4, c));
Printf (" A4 sizeof % d \ n ", sizeof (A4));
printf("\n");
Printf (" A8. A % d \ n ", field_offset (A8, a));
Printf (" A8. B % d \ n ", field_offset (A8, b));
Printf (" A8. C % d \ n ", field_offset (A8, c));
Printf (" A8 sizeof % d \ n ", sizeof (A8));
printf("\n");
Printf (" A16. A % d \ n ", field_offset A16, (a));
Printf (" A16. B % d \ n ", field_offset (A16, b));
Printf (" A16. C % d \ n ", field_offset (A16, c));
A16 sizeof printf (" % d \ n ", sizeof (A16));
printf("\n");
return 0;
}
//AD. A 0
//AD. B 4
//AD. 24 c
//AD sizeof 32
//
//A1. A 0
//A1. B 4
//A1. 17 c
//A1 sizeof 25
//
//A2. A 0
//A2. B 4
//A2. 18 c
//A2 sizeof 26
//
//A4. A 0
//A4. B 4
//A4. 20 c
//A4 sizeof 28
//
//A8. A 0
//A8. B 4
//A8. 24 c
//A8 sizeof 32
//
//A16. A 0
//A16. B 4
//A16. 24 c
//A16 sizeof 32
//
//

CodePudding user response:

Size=the last element of the offset + current length is + alignment

CodePudding user response:

Positive solution, 4/f

CodePudding user response:

VC compiler default structure alignment is Z8 (8 bytes)

Can use the # pragma pack definition structure alignment bytes
# pragma pack (push, 1)//1 byte alignment

# pragma pack (pop)

CodePudding user response:

 struct test2 {
Char text [5].
Long number1.
Long long number2.
};

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related