Skip to content

Commit 8fe7551

Browse files
author
Wang Jiazi
committed
When the object's memory layout is decided it can't be adjusted
1 parent ed79dd1 commit 8fe7551

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

C++/MemoryModel/packed.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
struct People
3+
{
4+
int age;
5+
char c;
6+
}__attribute__((packed));
7+
8+
9+
struct People_unpack
10+
{
11+
int age;
12+
char c;
13+
};
14+
15+
16+
//5 Bytes
17+
struct Student1
18+
{
19+
People p;
20+
};
21+
22+
//16 Bytes
23+
struct Student2
24+
{
25+
People p;
26+
double scores;
27+
};
28+
29+
//13 Bytes
30+
struct Student3
31+
{
32+
People p;
33+
double scores;
34+
}__attribute__((packed));
35+
36+
//16 Bytes
37+
struct Student4
38+
{
39+
People_unpack p;
40+
double scores;
41+
}__attribute__((packed));
42+
43+
int main()
44+
{
45+
struct People P;
46+
struct People_unpack P2;
47+
printf("%d %d sizeof(Student1)=%d, sizeof(Student2)=%d, sizeof(Student3)=%d, sizeof(Student4)=%d\n",sizeof(P), sizeof(P2),
48+
sizeof(Student1), sizeof(Student2), sizeof(Student3), sizeof(Student4));
49+
}

0 commit comments

Comments
 (0)