-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
61 lines (42 loc) · 1.02 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
How to Align The Struct
Description:
Demo of How to align the struct.
Use #pragma pack(1) precompilation directive!
Please see notes below.
**********************
Output:
Uart consumes 5 Bytes
**********************
Author: microgenios.com.br
Edited by j3
Date Jun, 21/2020
*/
#include <stdio.h>
#include <stdlib.h>
/*
If you comment '#pragma pack(1)'
The struct will consumes 8 bytes
The '#pragma pack(1)' forces the compilator to align the struct
If you run with '#pragma pack(1)'
The struct will consumes 5 Bytes
Try it for yourself!
Note: when you align the structure you gain in bytes :)
but lose in access time :/
*/
#pragma pack(1)
typedef struct Serial {
unsigned char ComPort; // X B B B
unsigned int BaudRate; // X X X X
}Serial_t;
int main()
{
Serial_t Uart =
{
.BaudRate = 9600,
.ComPort = 0
};
printf("Uart consumes %d Bytes", sizeof(Uart));
//system("pause");
return 0;
}