利用宏定義在編譯階段檢查結構體大小的方法

來源:互聯網
上載者:User

http://blog.csdn.net/crazycoder8848/article/details/8292770

=====================================================

typedef struct{    char a[100];} T_XXX;typedef struct{    char a[99];} T_QQQ; /* 檢測結構體的大小是否等於特定值 */#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]; \}/***    好了,現在開始,想檢查什麼,調用相應的宏即可。    如果結構大小不符合要求,則會編譯出錯。    注意,對宏的調用,不要寫在任何函數內  ***/SIZE_OF_TYPE_EQUAL_TO(T_XXX, 100)SIZE_OF_TYPE_UNEQUAL_TO(T_XXX, 99)SIZE_OF_TYPE_NOT_LARGER_THAN(T_XXX, 100)SIZE_OF_TYPE_NOT_SMALLER_THAN(T_QQQ, 98)SIZE_OF_TYPE_LARGER_THAN(T_QQQ, 96)SIZE_OF_TYPE_SMALLER_THAN(T_QQQ, 200)SIZE_OF_TYPE_LARGER_THAN2(T_QQQ, 96)SIZE_OF_TYPE_SMALLER_THAN2(T_QQQ, 200)SIZE_OF_TYPE_IS_MULTIPLE_OF(T_QQQ, 9) int main(){    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.