C语言-编译期间检查结构体的大小

编译期间检查结构体的大小

编程中,我们会定义一个结构体保存我们的数据.并且希望后续在增加/修改结构体的数据时,不会将结构体的size超过我们的限制值,或者希望已用的参数偏移是不能改动的.
如果size超过了限制,或者参数的偏移被改动,希望在编译时就进行报错,提醒开发者.

这里参考资料以及实践,得出了两个方法可以达到我们的需求:

  1. 方法一是利用编译器不能定义长度为负数的数组的特性;
  2. 方法二仅适用于C99的新特性,利用static_assert 宏进行报错;

在编译器使用C99以上时,更推荐方法二

方法一

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

/**注意,对宏的调用,不要写在任何函数内**/

/* 检测结构体的大小是否等于特定值 */
#define SIZE_OF_TYPE_EQUAL_TO(type, size)                   \
    static inline char size_of_##type##_equal_to_##size() { \
        char __dummy1[sizeof(type) - size];                 \
        char __dummy2[size - sizeof(type)];                 \
        return __dummy1[-1] + __dummy2[-1];                 \
    }

/* 检测结构体的大小是否不等于特定值 */
#define SIZE_OF_TYPE_UNEQUAL_TO(type, size)                   \
    static inline char size_of_##type##_unequal_to_##size() { \
        char __dummy1[0 == (10 / (sizeof(type) - size))];     \
        return __dummy1[-1];                                  \
    }

/* 检测结构体的大小是否不大于特定值 */
#define SIZE_OF_TYPE_NOT_LARGER_THAN(type, size)                   \
    static inline char size_of_##type##_not_larger_than_##size() { \
        char __dummy1[size - sizeof(type)];                        \
        return __dummy1[-1];                                       \
    }

/* 检测结构体的大小是否不小于特定值 */
#define SIZE_OF_TYPE_NOT_SMALLER_THAN(type, size)                   \
    static inline char size_of_##type##_not_smaller_than_##size() { \
        char __dummy1[sizeof(type) - size];                         \
        return __dummy1[-1];                                        \
    }

/* 检测结构体的大小是否小于特定值 */
#define SIZE_OF_TYPE_SMALLER_THAN(type, size) \
    SIZE_OF_TYPE_NOT_LARGER_THAN(type, size)  \
    SIZE_OF_TYPE_UNEQUAL_TO(type, size)

/* 检测结构体的大小是否大于特定值 */
#define SIZE_OF_TYPE_LARGER_THAN(type, size)  \
    SIZE_OF_TYPE_NOT_SMALLER_THAN(type, size) \
    SIZE_OF_TYPE_UNEQUAL_TO(type, size)

/* 检测结构体的大小是否小于特定值 版本2 */
#define SIZE_OF_TYPE_SMALLER_THAN2(type, size)                   \
    static inline char size_of_##type##_smaller_than2_##size() { \
        char __dummy1[size - sizeof(type) - 1];                  \
        return __dummy1[-1];                                     \
    }

/* 检测结构体的大小是否大于特定值 版本2 */
#define SIZE_OF_TYPE_LARGER_THAN2(type, size)                   \
    static inline char size_of_##type##_larger_than2_##size() { \
        char __dummy1[sizeof(type) - size - 1];                 \
        return __dummy1[-1];                                    \
    }

/* 检测结构体的大小是否为特定值的整数倍 */
#define SIZE_OF_TYPE_IS_MULTIPLE_OF(type, size)                   \
    static inline char size_of_##type##_is_multiple_of_##size() { \
        char __dummy1[0 - (sizeof(type) % size)];                 \
        return __dummy1[-1];                                      \
    }

/* 编译阶段检测结构体成员在结构体的偏移是否等于特定值 */
#define OFFSET_OF_TYPE_EQUAL_TO(type, member, size)                         \
    static inline char OFFSET_of_##member##_in_##type##_equal_to_##size() { \
        char __dummy1[offsetof(type, member) - size];                       \
        char __dummy2[size - offsetof(type, member)];                       \
        return __dummy1[-1] + __dummy2[-1];                                 \
    }

#define MAX_SIZE 70

// 10bytes
typedef struct {
    uint8_t a[5];
    uint8_t b[5];
} __attribute__((packed)) param1_t;

// 20bytes
typedef struct {
    uint8_t p[10];
    uint8_t res[10];
} __attribute__((packed)) param2_t;

// 40bytes
typedef struct {
    uint8_t p[20];
    uint8_t res[20];
} __attribute__((packed)) param3_t;

typedef struct {
    param1_t param1;  // offset: 0
    param2_t param2;  // offset: 10
    param3_t param3;  // offset: 20
} __attribute__((packed)) devConfig_t;

/*检测devConfig_t结构体是否与我们确定的大小一样*/
SIZE_OF_TYPE_EQUAL_TO(devConfig_t, MAX_SIZE)

/*检测结构体devConfig中的每个参数的偏移地址是否与我们确定的一样,
防止中间有人改变结构体大小,导致整个大结构体错位*/
OFFSET_OF_TYPE_EQUAL_TO(devConfig_t, param1, 0)
OFFSET_OF_TYPE_EQUAL_TO(devConfig_t, param2, 10)
OFFSET_OF_TYPE_EQUAL_TO(devConfig_t, param3, 30)

int main() {
    return 0;
}

当我们更改了MAX_SIZE的大小,编译则会提示: size of array ‘__dummy1’ is too large,这就是利用了编译器不能定义长度为负数组的特性

╰─❯ gcc test.c
test.c: In function ‘size_of_devConfig_t_equal_to_MAX_SIZE’:
test.c:12:14: error: size of array ‘__dummy1’ is too large
   12 |         char __dummy1[sizeof(type) - size];                 \
      |              ^~~~~~~~
test.c:104:1: note: in expansion of macro ‘SIZE_OF_TYPE_EQUAL_TO’
  104 | SIZE_OF_TYPE_EQUAL_TO(devConfig_t, MAX_SIZE)

方法二

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

#define MAX_SIZE 70

// 10bytes
typedef struct {
    uint8_t a[5];
    uint8_t b[5];
} __attribute__((packed)) param1_t;

// 20bytes
typedef struct {
    uint8_t p[10];
    uint8_t res[10];
} __attribute__((packed)) param2_t;

// 40bytes
typedef struct {
    uint8_t p[20];
    uint8_t res[20];
} __attribute__((packed)) param3_t;

typedef struct {
    param1_t param1;  // offset: 0
    param2_t param2;  // offset: 10
    param3_t param3;  // offset: 20
} __attribute__((packed)) devConfig_t;

/*检测devConfig_t结构体是否与我们确定的大小一样*/
static_assert(sizeof(devConfig_t) == MAX_SIZE,
              "sizeof(devConfig_t) need to be equal to MAX_SIZE");

/*检测结构体devConfig中的每个参数的偏移地址是否与我们确定的一样,
防止中间有人改变结构体大小,导致整个大结构体错位*/
static_assert(offsetof(devConfig_t, param1) == 0,
              "offsetof(devConfig_t, param1) need to be equal to 0");
static_assert(offsetof(devConfig_t, param2) == 10,
              "offsetof(devConfig_t, param1) need to be equal to 10");
static_assert(offsetof(devConfig_t, param3) == 30,
              "offsetof(devConfig_t, param1) need to be equal to 30");

int main() {
    return 0;
}

当我们更改了MAX_SIZE的大小,编译则会提示: error: static assertion failed: "sizeof(devConfig_t) need to be equal to MAX_SIZE",
这就是我们自己的打印提示,非常好用.

╰─❯ gcc test.c
In file included from test.c:4:
test.c:116:1: error: static assertion failed: "sizeof(devConfig_t) need to be equal to MAX_SIZE"
  116 | static_assert(sizeof(devConfig_t) == MAX_SIZE, "sizeof(devConfig_t) need to be equal to MAX_SIZE");
      | ^~~~~~~~~~~~~

参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容