内存布局与对齐

  • ~1.62K 字
  1. 1. c语言小端程序内存对齐

c语言小端程序内存对齐

结构体对象和数据类型对象在内存里的布局以及hex值对应关系

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
// in hex
// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
// | 7 H | 6 | 5 | 4 | 3 | 2 | 1 | 0 L |
// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
// | Rer-R Rebnd | Rer-R Cmprs | Rer-L Rebnd | Rer-L Cmprs | Frn-R Rebnd | Frn-R Cmprs | Frn-L Rebnd | Frn-L Cmprs |
// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
struct gain_set_s {
uint8_t rear_right_rebound;
uint8_t rear_right_compress;
uint8_t rear_left_rebound;
uint8_t rear_left_compress;
uint8_t front_right_rebound;
uint8_t front_right_compress;
uint8_t front_left_rebound;
uint8_t front_left_compress;
};
typedef struct gain_set_s gain_set_t;
gain_set_t set = {
.rear_right_rebound = 0xe0,
.rear_right_compress = 0xd0,
.rear_left_rebound = 0xc0,
.rear_left_compress = 0xb0,
.front_right_rebound = 0xa0,
.front_right_compress = 0x90,
.front_left_rebound = 0x80,
.front_left_compress = 0x70,
};
uint64_t tmp = *(uint64_t*)&set;
info("temp set is : %lx", tmp); // 转换为uint64后,hex为:0x708090a0b0c0d0e0;

log_arrary_desc((uint8_t*)&set, 8, LOG_ARR_BYTE1, "temp set is :");
// src/main.c:157 main: temp set is ::e0 d0 c0 b0 a0 90 80 70
// 上面是按照uint8 进行内存打印的内存布局
// 目前可得结论为:单字节结构体,转换同等数值hex是反着来的,内存布局是正着的
打赏
打赏提示信息
分享
分享提示信息