Skip to content

Commit 98ed503

Browse files
Update README.md
1 parent 4affe00 commit 98ed503

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

Data_Struct_Implementation/sizeof/README.md

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,68 @@ make
1414

1515
#define my_sizeof(type) (char*)(&type+1)-(char*)(&type)
1616

17-
// struct size 12 (3*sizeof(int))
17+
/* suppose this is a 32 bit machine, so a word (4 byte) alignment is required. */
18+
19+
// struct size 12 (3*sizeof(int)), p for padding bytes
1820
typedef struct Random_item {
19-
char a;
20-
int b;
21-
char c;
21+
char a; // 1
22+
// 3p
23+
int b; // 4 -> dominant item
24+
char c; // 1
25+
// 3p to satisfy processor word alignment requirement
2226
} Random_item;
2327

2428
// struct size 12
2529
typedef struct Random_item2 {
26-
char a;
27-
int b;
28-
char c;
29-
char d;
30-
uint16_t e;
30+
char a; // 1
31+
// 3
32+
int b; // 4 -> dominant item
33+
char c; // 1
34+
char d; // 1
35+
uint16_t e; // 2
3136
} Random_item2;
3237

3338
// struct size 24 (3*sizeof(long int))
3439
typedef struct Random_item3 {
35-
int b;
36-
char d;
37-
long int a;
38-
int c;
39-
uint16_t f;
40+
int b; // 4
41+
char d; // 1
42+
// 3p
43+
long int a; // 8 -> if long int is 8byte, then 64 bit machine
44+
int c; // 4
45+
uint16_t f; // 2
46+
// 2p
4047
} Random_item3;
4148

49+
// struct size 32 (4*sizeof(long int))
50+
typedef struct Random_item4 {
51+
int b; // 4
52+
char d; // 1
53+
// 3p
54+
long int a; // 8 -> if long int is 8byte, then 64 bit machine
55+
int c; // 4
56+
uint16_t f; // 2
57+
char e; // 1
58+
char g; // 1
59+
char h; // 1
60+
// 7p -> to align with 8 byte long int earilier
61+
} Random_item4;
62+
63+
// struct size 56
64+
typedef struct Random_item5 {
65+
int b; // 4
66+
char d; // 1
67+
// 3p
68+
long int a; // 8
69+
int c; // 4
70+
uint16_t f; // 2
71+
char e; // 1
72+
// 1p
73+
Random_item3 random; // 24 <--- we need to break the structure up, otherwise it is hard to judge the padding in between
74+
char g; // 1
75+
char h; // 1
76+
// 6p -> to align with 8 byte long int earilier
77+
} Random_item5;
78+
4279
// should output 4 or -4
4380
void argument_alignment_check( char c1, char c2 )
4481
{

0 commit comments

Comments
 (0)