c_cpp:宏定义

  • ~34.78K 字
  1. 1. printf 宏,实现多样式打印
    1. 1.1. 更全能的内存打印
      1. 1.1.1. V1版本
      2. 1.1.2. V2

printf 宏,实现多样式打印

由黑魔法令该得来,在rtt 和 nrf log的基础上实现

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
typedef enum
{
LOG_ARR_BYTE1,
LOG_ARR_BYTE2,
LOG_ARR_BYTE4,
LOG_ARR_FLOAT,
LOG_ARR_INT,
} LogArrayType_e;

#define log(...) \
do \
{ \
printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
printf(__VA_ARGS__); \
printf("\r\n"); \
} while (0);

#define log_arrary(pArr, len, logArrayType) \
do \
{ \
for (int i = 0; i < len; i++) \
{ \
switch (logArrayType) \
{ \
case LOG_ARR_BYTE1: \
printf("%02x ", *(uint8_t *)(pArr + i)); \
break; \
case LOG_ARR_BYTE2: \
printf("%04x ", *(uint16_t *)(pArr + i)); \
break; \
case LOG_ARR_BYTE4: \
printf("%08x ", *(uint32_t *)(pArr + i)); \
break; \
case LOG_ARR_FLOAT: \
printf("%f ", (double)*(float *)(pArr + i)); \
break; \
case LOG_ARR_INT: \
printf("%d ", *(int *)(pArr + i)); \
break; \
default: \
break; \
} \
} \
printf("\r\n"); \
} while (0);

#define log_arrary_desc(pArr, len, logArrayType, desc) \
printf("%s:%d %s: %s:", __FILE__, __LINE__, __func__, desc); \
do \
{ \
for (int i = 0; i < len; i++) \
{ \
switch (logArrayType) \
{ \
case LOG_ARR_BYTE1: \
printf("%02x ", *(uint8_t *)(pArr + i)); \
break; \
case LOG_ARR_BYTE2: \
printf("%04x ", *(uint16_t *)(pArr + i)); \
break; \
case LOG_ARR_BYTE4: \
printf("%08x ", *(uint32_t *)(pArr + i)); \
break; \
case LOG_ARR_FLOAT: \
printf("%f ", (double)*(float *)(pArr + i)); \
break; \
case LOG_ARR_INT: \
printf("%d ", *(int *)(pArr + i)); \
break; \
default: \
break; \
} \
} \
printf("\r\n"); \
} while (0);

#define log_memory(pMem, size, desc) \
do { \
printf("%s:%d %s: %s 000000 ", __FILE__, __LINE__, __func__, desc);\
uint8_t *byte_ptr = (uint8_t *)(pMem); \
size_t size_a = (size/8+1)*8, a = 1; \
for (size_t i = 1; i <= size_a; i++) { \
printf("%02x ", byte_ptr[i-1]); \
if (i % 8 == 0) printf("\t"); \
if (i % 16 == 0) printf("\r\n%s:%d %s: %s %06lx ", __FILE__, __LINE__, __func__, desc, a); \
a++; \
} \
printf("\r\n"); \
} while (0)

#define log_struct(pStruct, desc) \
do { \
printf("%s:%d %s: %s ", __FILE__, __LINE__, __func__, desc); \
uint8_t *byte_ptr = (uint8_t *)(pStruct); \
for (size_t i = 0; i < sizeof(*(pStruct)); i++) { \
printf("%02x ", byte_ptr[i]); \
} \
printf("\r\n"); \
} while (0)

更全能的内存打印

V1版本

  1. mem_print.h
    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    #ifndef MEM_PRINT_H
    #define MEM_PRINT_H

    #include <stdio.h>
    #include <stdint.h>
    #include <string.h>
    #include <stddef.h>

    // 基本内存打印宏
    #define PRINT_MEM(addr, len) do { \
    printf("Memory at %p (%zu bytes):\n", (void*)(addr), (size_t)(len)); \
    print_memory_hex((const uint8_t*)(addr), (size_t)(len)); \
    } while(0)

    // 高级内存打印(带地址偏移和ASCII)
    #define PRINT_MEM_ADV(addr, len) do { \
    printf("Memory at %p (%zu bytes):\n", (void*)(addr), (size_t)(len)); \
    print_memory_advanced((const uint8_t*)(addr), (size_t)(len)); \
    } while(0)

    // 按字节长度拼接打印
    #define PRINT_MEM_BY_SIZE(addr, len, byte_size) do { \
    printf("Memory at %p (%zu bytes, %zu-byte chunks):\n", \
    (void*)(addr), (size_t)(len), (size_t)(byte_size)); \
    print_memory_by_size((const uint8_t*)(addr), (size_t)(len), (size_t)(byte_size)); \
    } while(0)

    // 按数据类型打印
    #define PRINT_MEM_AS_TYPE(addr, len, type) do { \
    printf("Memory at %p as " #type " (%zu elements):\n", \
    (void*)(addr), (size_t)(len) / sizeof(type)); \
    print_memory_as_type((const uint8_t*)(addr), (size_t)(len), #type); \
    } while(0)

    // 通用结构体打印宏
    #define PRINT_STRUCT(ptr, type) do { \
    printf("Structure " #type " at %p (size: %zu bytes):\n", \
    (void*)(ptr), sizeof(type)); \
    print_struct_generic((const uint8_t*)(ptr), #type, sizeof(type)); \
    } while(0)

    // 注册结构体布局信息
    #define REGISTER_STRUCT(type, desc) \
    register_struct_layout(#type, desc, sizeof(type))

    // 结构体成员信息
    typedef struct {
    const char* name;
    size_t offset;
    size_t size;
    const char* type;
    } struct_member_t;

    // 结构体布局描述
    typedef struct {
    const char* struct_name;
    struct_member_t* members;
    size_t member_count;
    size_t struct_size;
    } struct_layout_t;

    // 函数声明
    void print_memory_hex(const uint8_t* data, size_t len);
    void print_memory_advanced(const uint8_t* data, size_t len);
    void print_memory_by_size(const uint8_t* data, size_t len, size_t byte_size);
    void print_memory_as_type(const uint8_t* data, size_t len, const char* type_name);
    void print_struct_generic(const uint8_t* data, const char* struct_name, size_t struct_size);
    size_t get_type_size(const char* type_name);
    void print_single_element(const uint8_t* data, const char* type_name);
    void register_struct_layout(const char* struct_name, struct_member_t* members, size_t member_count);
    void init_struct_registry(void);
    void cleanup_struct_registry(void);

    // 内置类型支持
    #define TYPE_INT8 "int8_t"
    #define TYPE_UINT8 "uint8_t"
    #define TYPE_INT16 "int16_t"
    #define TYPE_UINT16 "uint16_t"
    #define TYPE_INT32 "int32_t"
    #define TYPE_UINT32 "uint32_t"
    #define TYPE_FLOAT "float"
    #define TYPE_DOUBLE "double"
    #define TYPE_CHAR "char"

    #endif // MEM_PRINT_H
  2. mem_print.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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
       #include "mem_print.h"
    #include <stdlib.h>

    #define MAX_STRUCT_LAYOUTS 32

    static struct_layout_t* struct_registry[MAX_STRUCT_LAYOUTS];
    static size_t struct_registry_count = 0;

    void print_memory_hex(const uint8_t* data, size_t len) {
    for (size_t i = 0; i < len; i++) {
    printf("%02X ", data[i]);
    if ((i + 1) % 16 == 0) printf("\n");
    }
    if (len % 16 != 0) printf("\n");
    }

    void print_memory_advanced(const uint8_t* data, size_t len) {
    const uint8_t* base_addr = data;

    for (size_t i = 0; i < len; i += 16) {
    // 打印地址偏移
    printf("%08zX: ", i);

    // 打印十六进制
    for (size_t j = 0; j < 16; j++) {
    if (i + j < len) {
    printf("%02X ", data[i + j]);
    } else {
    printf(" "); // 对齐空格
    }

    if (j == 7) printf(" "); // 中间分隔
    }

    printf(" ");

    // 打印ASCII字符
    for (size_t j = 0; j < 16; j++) {
    if (i + j < len) {
    uint8_t c = data[i + j];
    if (c >= 32 && c <= 126) { // 可打印字符
    printf("%c", c);
    } else {
    printf("."); // 不可打印字符用点代替
    }
    } else {
    printf(" ");
    }
    }
    printf("\n");
    }
    }

    void print_memory_by_size(const uint8_t* data, size_t len, size_t byte_size) {
    size_t elements = len / byte_size;
    const uint8_t* base_addr = data;

    for (size_t i = 0; i < elements; i++) {
    const uint8_t* element_addr = data + i * byte_size;
    printf("%08zX: ", (size_t)(element_addr - base_addr));

    switch (byte_size) {
    case 1:
    printf("0x%02X", *(const uint8_t*)element_addr);
    break;
    case 2:
    printf("0x%04X", *(const uint16_t*)element_addr);
    break;
    case 4:
    printf("0x%08X", *(const uint32_t*)element_addr);
    break;
    case 8:
    printf("0x%016llX", *(const uint64_t*)element_addr);
    break;
    default:
    // 对于不支持的字节大小,直接打印原始字节
    printf("Raw: ");
    for (size_t j = 0; j < byte_size; j++) {
    printf("%02X", element_addr[j]);
    }
    break;
    }
    printf("\n");
    }
    }

    size_t get_type_size(const char* type_name) {
    if (strcmp(type_name, TYPE_INT8) == 0 ||
    strcmp(type_name, TYPE_UINT8) == 0 ||
    strcmp(type_name, TYPE_CHAR) == 0)
    return 1;

    if (strcmp(type_name, TYPE_INT16) == 0 ||
    strcmp(type_name, TYPE_UINT16) == 0)
    return 2;

    if (strcmp(type_name, TYPE_INT32) == 0 ||
    strcmp(type_name, TYPE_UINT32) == 0 ||
    strcmp(type_name, TYPE_FLOAT) == 0)
    return 4;

    if (strcmp(type_name, TYPE_DOUBLE) == 0)
    return 8;

    // 检查注册的结构体
    for (size_t i = 0; i < struct_registry_count; i++) {
    if (strcmp(struct_registry[i]->struct_name, type_name) == 0) {
    return struct_registry[i]->struct_size;
    }
    }

    return 1; // 默认1字节
    }

    void print_single_element(const uint8_t* data, const char* type_name) {
    if (strcmp(type_name, TYPE_INT8) == 0)
    printf("%d", *(const int8_t*)data);
    else if (strcmp(type_name, TYPE_UINT8) == 0)
    printf("%u", *(const uint8_t*)data);
    else if (strcmp(type_name, TYPE_INT16) == 0)
    printf("%d", *(const int16_t*)data);
    else if (strcmp(type_name, TYPE_UINT16) == 0)
    printf("%u", *(const uint16_t*)data);
    else if (strcmp(type_name, TYPE_INT32) == 0)
    printf("%d", *(const int32_t*)data);
    else if (strcmp(type_name, TYPE_UINT32) == 0)
    printf("%u", *(const uint32_t*)data);
    else if (strcmp(type_name, TYPE_FLOAT) == 0)
    printf("%.6f", *(const float*)data);
    else if (strcmp(type_name, TYPE_DOUBLE) == 0)
    printf("%.6lf", *(const double*)data);
    else if (strcmp(type_name, TYPE_CHAR) == 0)
    printf("'%c'", *(const char*)data);
    else
    printf("0x%02X", *data);
    }

    void print_memory_as_type(const uint8_t* data, size_t len, const char* type_name) {
    size_t type_size = get_type_size(type_name);
    size_t elements = len / type_size;
    const uint8_t* base_addr = data;

    for (size_t i = 0; i < elements; i++) {
    const uint8_t* element_addr = data + i * type_size;
    printf("%08zX: ", (size_t)(element_addr - base_addr));
    print_single_element(element_addr, type_name);
    printf("\n");
    }
    }

    void register_struct_layout(const char* struct_name, struct_member_t* members, size_t member_count) {
    if (struct_registry_count >= MAX_STRUCT_LAYOUTS) {
    fprintf(stderr, "Error: Struct registry full\n");
    return;
    }

    struct_layout_t* layout = malloc(sizeof(struct_layout_t));
    layout->struct_name = struct_name;
    layout->members = members;
    layout->member_count = member_count;

    // 计算结构体大小(假设最后一个成员决定大小)
    size_t max_offset = 0;
    size_t max_size = 0;
    for (size_t i = 0; i < member_count; i++) {
    size_t member_end = members[i].offset + members[i].size;
    if (member_end > max_offset) {
    max_offset = member_end;
    max_size = member_end;
    }
    }
    layout->struct_size = max_size;

    struct_registry[struct_registry_count++] = layout;
    }

    void print_struct_generic(const uint8_t* data, const char* struct_name, size_t struct_size) {
    // 查找注册的结构体布局
    struct_layout_t* layout = NULL;
    for (size_t i = 0; i < struct_registry_count; i++) {
    if (strcmp(struct_registry[i]->struct_name, struct_name) == 0) {
    layout = struct_registry[i];
    break;
    }
    }

    if (layout) {
    // 打印结构体成员信息
    printf("Member layout:\n");
    for (size_t i = 0; i < layout->member_count; i++) {
    struct_member_t* member = &layout->members[i];
    const uint8_t* member_addr = data + member->offset;

    printf(" +%04zX: %-12s %-10s = ",
    member->offset, member->name, member->type);

    // 根据类型打印成员值
    if (strcmp(member->type, TYPE_INT32) == 0)
    printf("%d", *(const int32_t*)member_addr);
    else if (strcmp(member->type, TYPE_UINT32) == 0)
    printf("%u", *(const uint32_t*)member_addr);
    else if (strcmp(member->type, TYPE_INT16) == 0)
    printf("%d", *(const int16_t*)member_addr);
    else if (strcmp(member->type, TYPE_UINT16) == 0)
    printf("%u", *(const uint16_t*)member_addr);
    else if (strcmp(member->type, TYPE_FLOAT) == 0)
    printf("%.2f", *(const float*)member_addr);
    else if (strcmp(member->type, TYPE_DOUBLE) == 0)
    printf("%.2lf", *(const double*)member_addr);
    else if (strcmp(member->type, TYPE_CHAR) == 0) {
    printf("'%c'", *(const char*)member_addr);
    if (member->size > 1) {
    printf(" (string: \"");
    for (size_t j = 0; j < member->size && member_addr[j] != '\0'; j++) {
    printf("%c", member_addr[j]);
    }
    printf("\")");
    }
    }
    else {
    // 未知类型,打印原始字节
    printf("0x");
    for (size_t j = 0; j < member->size; j++) {
    printf("%02X", member_addr[j]);
    }
    }
    printf("\n");
    }
    printf("\n");
    }

    // 总是打印原始内存布局
    printf("Raw memory layout:\n");
    print_memory_advanced(data, struct_size);
    }

    void init_struct_registry(void) {
    struct_registry_count = 0;
    for (int i = 0; i < MAX_STRUCT_LAYOUTS; i++) {
    struct_registry[i] = NULL;
    }
    }

    void cleanup_struct_registry(void) {
    for (size_t i = 0; i < struct_registry_count; i++) {
    free(struct_registry[i]);
    }
    struct_registry_count = 0;
    }
    ```
    3. main.c
    ```c
    #include "mem_print.h"
    #include <stddef.h>

    // 示例结构体定义
    typedef struct {
    int32_t id;
    float score;
    char name[16];
    uint16_t flags;
    double timestamp;
    } example_struct_t;

    // 另一个示例结构体
    typedef struct {
    uint8_t version;
    uint32_t checksum;
    char tag[8];
    int16_t values[4];
    } complex_struct_t;

    // 注册example_struct_t的布局信息
    static struct_member_t example_members[] = {
    {"id", offsetof(example_struct_t, id), sizeof(int32_t), TYPE_INT32},
    {"score", offsetof(example_struct_t, score), sizeof(float), TYPE_FLOAT},
    {"name", offsetof(example_struct_t, name), 16, TYPE_CHAR},
    {"flags", offsetof(example_struct_t, flags), sizeof(uint16_t), TYPE_UINT16},
    {"timestamp", offsetof(example_struct_t, timestamp), sizeof(double), TYPE_DOUBLE}
    };

    // 注册complex_struct_t的布局信息
    static struct_member_t complex_members[] = {
    {"version", offsetof(complex_struct_t, version), sizeof(uint8_t), TYPE_UINT8},
    {"checksum", offsetof(complex_struct_t, checksum), sizeof(uint32_t), TYPE_UINT32},
    {"tag", offsetof(complex_struct_t, tag), 8, TYPE_CHAR},
    {"values", offsetof(complex_struct_t, values), sizeof(int16_t) * 4, TYPE_INT16}
    };

    int main() {
    // 初始化结构体注册表
    init_struct_registry();

    // 注册结构体布局
    REGISTER_STRUCT(example_struct_t, example_members);
    REGISTER_STRUCT(complex_struct_t, complex_members);

    // 测试数据
    printf("=== 测试数据初始化 ===\n");
    uint8_t test_data[] = {
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
    0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
    };

    int32_t int_data[] = {100, -200, 300, -400, 500};
    float float_data[] = {1.1f, -2.2f, 3.3f, -4.4f, 5.5f};

    printf("=== 基本内存布局打印 ===\n");
    PRINT_MEM(test_data, sizeof(test_data));

    printf("\n=== 高级内存打印(带地址偏移和ASCII) ===\n");
    PRINT_MEM_ADV(test_data, sizeof(test_data));

    printf("\n=== 按字节长度拼接打印 ===\n");
    PRINT_MEM_BY_SIZE(test_data, sizeof(test_data), 1); // 1字节
    printf("---\n");
    PRINT_MEM_BY_SIZE(test_data, sizeof(test_data), 2); // 2字节
    printf("---\n");
    PRINT_MEM_BY_SIZE(test_data, sizeof(test_data), 4); // 4字节
    printf("---\n");
    PRINT_MEM_BY_SIZE(test_data, sizeof(test_data), 8); // 8字节

    printf("\n=== 按数据类型打印 ===\n");
    PRINT_MEM_AS_TYPE(int_data, sizeof(int_data), int32_t);
    printf("---\n");
    PRINT_MEM_AS_TYPE(float_data, sizeof(float_data), float);

    printf("\n=== 结构体打印演示 ===\n");

    // 创建测试结构体
    example_struct_t test_struct = {
    .id = 12345,
    .score = 95.5f,
    .name = "Hello World!",
    .flags = 0xABCD,
    .timestamp = 123456789.123
    };

    complex_struct_t complex_struct = {
    .version = 2,
    .checksum = 0xDEADBEEF,
    .tag = "TEST",
    .values = {100, -200, 300, -400}
    };

    printf("--- example_struct_t ---\n");
    PRINT_STRUCT(&test_struct, example_struct_t);

    printf("--- complex_struct_t ---\n");
    PRINT_STRUCT(&complex_struct, complex_struct_t);

    printf("\n=== 数组结构体演示 ===\n");
    example_struct_t struct_array[2] = {
    {1, 80.5f, "First", 0x1111, 111.111},
    {2, 90.5f, "Second", 0x2222, 222.222}
    };

    PRINT_MEM_AS_TYPE(struct_array, sizeof(struct_array), example_struct_t);

    // 清理资源
    cleanup_struct_registry();

    return 0;
    }

V2

  1. mem_print.h
    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
    #ifndef MEM_PRINT_H
    #define MEM_PRINT_H

    #include <stdio.h>
    #include <stdint.h>
    #include <string.h>
    #include <stddef.h>

    // 基本内存打印宏
    #define PRINT_MEM(addr, len) do { \
    printf("Memory at %p (%zu bytes):\n", (void*)(addr), (size_t)(len)); \
    print_memory_hex((const uint8_t*)(addr), (size_t)(len)); \
    } while(0)

    // 高级内存打印(带地址偏移和ASCII)
    #define PRINT_MEM_ADV(addr, len) do { \
    printf("Memory at %p (%zu bytes):\n", (void*)(addr), (size_t)(len)); \
    print_memory_advanced((const uint8_t*)(addr), (size_t)(len)); \
    } while(0)

    // 按字节长度拼接打印
    #define PRINT_MEM_BY_SIZE(addr, len, byte_size) do { \
    printf("Memory at %p (%zu bytes, %zu-byte chunks):\n", \
    (void*)(addr), (size_t)(len), (size_t)(byte_size)); \
    print_memory_by_size((const uint8_t*)(addr), (size_t)(len), (size_t)(byte_size)); \
    } while(0)

    // 按数据类型打印
    #define PRINT_MEM_AS_TYPE(addr, len, type) do { \
    printf("Memory at %p as " #type " (%zu elements):\n", \
    (void*)(addr), (size_t)(len) / sizeof(type)); \
    print_memory_as_type((const uint8_t*)(addr), (size_t)(len), #type); \
    } while(0)

    // 直接结构体打印宏 - 无需注册!
    #define PRINT_STRUCT(ptr) do { \
    printf("Structure at %p (size: %zu bytes):\n", \
    (void*)(ptr), sizeof(*(ptr))); \
    print_memory_advanced((const uint8_t*)(ptr), sizeof(*(ptr))); \
    } while(0)

    // 增强版结构体打印(带类型名)
    #define PRINT_STRUCT_T(ptr, type) do { \
    printf("Structure " #type " at %p (size: %zu bytes):\n", \
    (void*)(ptr), sizeof(type)); \
    print_memory_advanced((const uint8_t*)(ptr), sizeof(type)); \
    } while(0)

    // 函数声明
    void print_memory_hex(const uint8_t* data, size_t len);
    void print_memory_advanced(const uint8_t* data, size_t len);
    void print_memory_by_size(const uint8_t* data, size_t len, size_t byte_size);
    void print_memory_as_type(const uint8_t* data, size_t len, const char* type_name);
    size_t get_type_size(const char* type_name);
    void print_single_element(const uint8_t* data, const char* type_name);

    #endif // MEM_PRINT_H
  2. mem_print.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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    #include "mem_print.h"
    #include <stdlib.h>

    void print_memory_hex(const uint8_t* data, size_t len) {
    for (size_t i = 0; i < len; i++) {
    printf("%02X ", data[i]);
    if ((i + 1) % 16 == 0) printf("\n");
    }
    if (len % 16 != 0) printf("\n");
    }

    void print_memory_advanced(const uint8_t* data, size_t len) {
    const uint8_t* base_addr = data;

    for (size_t i = 0; i < len; i += 16) {
    // 打印地址偏移
    printf("%08zX: ", i);

    // 打印十六进制
    for (size_t j = 0; j < 16; j++) {
    if (i + j < len) {
    printf("%02X ", data[i + j]);
    } else {
    printf(" "); // 对齐空格
    }

    if (j == 7) printf(" "); // 中间分隔
    }

    printf(" ");

    // 打印ASCII字符
    for (size_t j = 0; j < 16; j++) {
    if (i + j < len) {
    uint8_t c = data[i + j];
    if (c >= 32 && c <= 126) { // 可打印字符
    printf("%c", c);
    } else {
    printf("."); // 不可打印字符用点代替
    }
    } else {
    printf(" ");
    }
    }
    printf("\n");
    }
    }

    void print_memory_by_size(const uint8_t* data, size_t len, size_t byte_size) {
    size_t elements = len / byte_size;
    const uint8_t* base_addr = data;

    for (size_t i = 0; i < elements; i++) {
    const uint8_t* element_addr = data + i * byte_size;
    printf("%08zX: ", (size_t)(element_addr - base_addr));

    switch (byte_size) {
    case 1:
    printf("0x%02X", *(const uint8_t*)element_addr);
    break;
    case 2:
    printf("0x%04X", *(const uint16_t*)element_addr);
    break;
    case 4:
    printf("0x%08X", *(const uint32_t*)element_addr);
    break;
    case 8:
    printf("0x%016llX", *(const uint64_t*)element_addr);
    break;
    default:
    // 对于不支持的字节大小,直接打印原始字节
    printf("Raw: ");
    for (size_t j = 0; j < byte_size; j++) {
    printf("%02X", element_addr[j]);
    }
    break;
    }
    printf("\n");
    }
    }

    size_t get_type_size(const char* type_name) {
    if (strcmp(type_name, "int8_t") == 0 ||
    strcmp(type_name, "uint8_t") == 0 ||
    strcmp(type_name, "char") == 0)
    return 1;

    if (strcmp(type_name, "int16_t") == 0 ||
    strcmp(type_name, "uint16_t") == 0)
    return 2;

    if (strcmp(type_name, "int32_t") == 0 ||
    strcmp(type_name, "uint32_t") == 0 ||
    strcmp(type_name, "float") == 0)
    return 4;

    if (strcmp(type_name, "double") == 0)
    return 8;

    return 1; // 默认1字节
    }

    void print_single_element(const uint8_t* data, const char* type_name) {
    if (strcmp(type_name, "int8_t") == 0)
    printf("%d", *(const int8_t*)data);
    else if (strcmp(type_name, "uint8_t") == 0)
    printf("%u", *(const uint8_t*)data);
    else if (strcmp(type_name, "int16_t") == 0)
    printf("%d", *(const int16_t*)data);
    else if (strcmp(type_name, "uint16_t") == 0)
    printf("%u", *(const uint16_t*)data);
    else if (strcmp(type_name, "int32_t") == 0)
    printf("%d", *(const int32_t*)data);
    else if (strcmp(type_name, "uint32_t") == 0)
    printf("%u", *(const uint32_t*)data);
    else if (strcmp(type_name, "float") == 0)
    printf("%.6f", *(const float*)data);
    else if (strcmp(type_name, "double") == 0)
    printf("%.6lf", *(const double*)data);
    else if (strcmp(type_name, "char") == 0)
    printf("'%c'", *(const char*)data);
    else
    printf("0x%02X", *data);
    }

    void print_memory_as_type(const uint8_t* data, size_t len, const char* type_name) {
    size_t type_size = get_type_size(type_name);
    size_t elements = len / type_size;
    const uint8_t* base_addr = data;

    for (size_t i = 0; i < elements; i++) {
    const uint8_t* element_addr = data + i * type_size;
    printf("%08zX: ", (size_t)(element_addr - base_addr));
    print_single_element(element_addr, type_name);
    printf("\n");
    }
    }
  3. 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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    #include "mem_print.h"
    #include <stddef.h>

    // 示例结构体定义
    typedef struct {
    int32_t id;
    float score;
    char name[16];
    uint16_t flags;
    double timestamp;
    } example_struct_t;

    // 另一个示例结构体
    typedef struct {
    uint8_t version;
    uint32_t checksum;
    char tag[8];
    int16_t values[4];
    example_struct_t nested; // 嵌套结构体
    } complex_struct_t;

    // 简单结构体
    typedef struct {
    char a;
    int b;
    short c;
    } simple_struct_t;

    int main() {
    printf("=== 简化版内存打印演示 ===\n");
    printf("无需注册,直接打印任意结构体!\n\n");

    // 测试数据
    uint8_t test_data[] = {
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
    0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68
    };

    int32_t int_data[] = {100, -200, 300, -400, 500};
    float float_data[] = {1.1f, -2.2f, 3.3f, -4.4f, 5.5f};

    printf("=== 基本内存布局打印 ===\n");
    PRINT_MEM(test_data, sizeof(test_data));

    printf("\n=== 高级内存打印(带地址偏移和ASCII) ===\n");
    PRINT_MEM_ADV(test_data, sizeof(test_data));

    printf("\n=== 按字节长度拼接打印 ===\n");
    PRINT_MEM_BY_SIZE(test_data, sizeof(test_data), 4);

    printf("\n=== 按数据类型打印 ===\n");
    PRINT_MEM_AS_TYPE(int_data, sizeof(int_data), int32_t);
    printf("---\n");
    PRINT_MEM_AS_TYPE(float_data, sizeof(float_data), float);

    printf("\n=== 直接结构体打印(无需注册!) ===\n");

    // 创建测试结构体
    example_struct_t test_struct = {
    .id = 12345,
    .score = 95.5f,
    .name = "Hello World!",
    .flags = 0xABCD,
    .timestamp = 123456789.123
    };

    complex_struct_t complex_struct = {
    .version = 2,
    .checksum = 0xDEADBEEF,
    .tag = "TEST",
    .values = {100, -200, 300, -400},
    .nested = test_struct
    };

    simple_struct_t simple_struct = {
    .a = 'X',
    .b = 999,
    .c = 123
    };

    printf("--- example_struct_t ---\n");
    PRINT_STRUCT(&test_struct); // 最简单用法

    printf("--- complex_struct_t ---\n");
    PRINT_STRUCT_T(&complex_struct, complex_struct_t); // 带类型名显示

    printf("--- simple_struct_t ---\n");
    PRINT_STRUCT(&simple_struct);

    printf("\n=== 数组结构体演示 ===\n");
    example_struct_t struct_array[2] = {
    {1, 80.5f, "First", 0x1111, 111.111},
    {2, 90.5f, "Second", 0x2222, 222.222}
    };

    PRINT_MEM_AS_TYPE(struct_array, sizeof(struct_array), example_struct_t);

    printf("\n=== 指针结构体演示 ===\n");
    example_struct_t *struct_ptr = &test_struct;
    PRINT_STRUCT(struct_ptr);

    return 0;
    }
打赏
打赏提示信息
分享
分享提示信息