編譯期間檢查結(jié)構(gòu)體的大小
編程中,我們會(huì)定義一個(gè)結(jié)構(gòu)體保存我們的數(shù)據(jù).并且希望后續(xù)在增加/修改結(jié)構(gòu)體的數(shù)據(jù)時(shí),不會(huì)將結(jié)構(gòu)體的size超過我們的限制值,或者希望已用的參數(shù)偏移是不能改動(dòng)的.
如果size超過了限制,或者參數(shù)的偏移被改動(dòng),希望在編譯時(shí)就進(jìn)行報(bào)錯(cuò),提醒開發(fā)者.
這里參考資料以及實(shí)踐,得出了兩個(gè)方法可以達(dá)到我們的需求:
- 方法一是利用編譯器不能定義長度為負(fù)數(shù)的數(shù)組的特性;
- 方法二僅適用于C99的新特性,利用static_assert 宏進(jìn)行報(bào)錯(cuò);
在編譯器使用C99以上時(shí),更推薦方法二
方法一
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
/**注意,對宏的調(diào)用,不要寫在任何函數(shù)內(nèi)**/
/* 檢測結(jié)構(gòu)體的大小是否等于特定值 */
#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]; \
}
/* 檢測結(jié)構(gòu)體的大小是否不等于特定值 */
#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]; \
}
/* 檢測結(jié)構(gòu)體的大小是否不大于特定值 */
#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]; \
}
/* 檢測結(jié)構(gòu)體的大小是否不小于特定值 */
#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]; \
}
/* 檢測結(jié)構(gòu)體的大小是否小于特定值 */
#define SIZE_OF_TYPE_SMALLER_THAN(type, size) \
SIZE_OF_TYPE_NOT_LARGER_THAN(type, size) \
SIZE_OF_TYPE_UNEQUAL_TO(type, size)
/* 檢測結(jié)構(gòu)體的大小是否大于特定值 */
#define SIZE_OF_TYPE_LARGER_THAN(type, size) \
SIZE_OF_TYPE_NOT_SMALLER_THAN(type, size) \
SIZE_OF_TYPE_UNEQUAL_TO(type, size)
/* 檢測結(jié)構(gòu)體的大小是否小于特定值 版本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]; \
}
/* 檢測結(jié)構(gòu)體的大小是否大于特定值 版本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]; \
}
/* 檢測結(jié)構(gòu)體的大小是否為特定值的整數(shù)倍 */
#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]; \
}
/* 編譯階段檢測結(jié)構(gòu)體成員在結(jié)構(gòu)體的偏移是否等于特定值 */
#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結(jié)構(gòu)體是否與我們確定的大小一樣*/
SIZE_OF_TYPE_EQUAL_TO(devConfig_t, MAX_SIZE)
/*檢測結(jié)構(gòu)體devConfig中的每個(gè)參數(shù)的偏移地址是否與我們確定的一樣,
防止中間有人改變結(jié)構(gòu)體大小,導(dǎo)致整個(gè)大結(jié)構(gòu)體錯(cuò)位*/
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;
}
當(dāng)我們更改了MAX_SIZE的大小,編譯則會(huì)提示: size of array ‘__dummy1’ is too large,這就是利用了編譯器不能定義長度為負(fù)數(shù)組的特性
╰─? 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結(jié)構(gòu)體是否與我們確定的大小一樣*/
static_assert(sizeof(devConfig_t) == MAX_SIZE,
"sizeof(devConfig_t) need to be equal to MAX_SIZE");
/*檢測結(jié)構(gòu)體devConfig中的每個(gè)參數(shù)的偏移地址是否與我們確定的一樣,
防止中間有人改變結(jié)構(gòu)體大小,導(dǎo)致整個(gè)大結(jié)構(gòu)體錯(cuò)位*/
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;
}
當(dāng)我們更改了MAX_SIZE的大小,編譯則會(huì)提示: 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");
| ^~~~~~~~~~~~~