亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 13396 | 回復(fù): 61
打印 上一主題 下一主題

C語言帶常量參數(shù)宏的編譯時參數(shù)檢查,有辦法實現(xiàn)嗎?(答案已經(jīng)公開) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2012-02-01 11:42 |只看該作者 |倒序瀏覽
本帖最后由 沒本 于 2012-02-01 18:45 編輯

定義一個帶參數(shù)的宏

  1. #define MACRO_P(A) \
  2. { \
  3.     some.a = A; \
  4.     some.b = A+1; \
  5.     some.c[A] = 0; \
  6. }
  7. /* 用法,參數(shù)約定為只能是整型常量,范圍在0..50 */
  8. MACRO_P(0);
  9. MACRO_P(33);
復(fù)制代碼
其中參數(shù)A使用時必為常量,且在0..50范圍內(nèi)。那么如何在編譯時判斷越界并報錯停止編譯,而不是在運行時?
有可能實現(xiàn)嗎?如果換成C++呢?

公開答案,其實就是用static_assert方法,這個方法是D語言最早官方支持,當(dāng)然C和C++也都實現(xiàn)了,能保證程序的正確性并且沒有運行時開銷:
1. C_1x標(biāo)準(zhǔn)提供了_Static_assert()。gcc 4.6以后開始支持。
文件s.c

  1. /* C_1x standard _Static_assert version */
  2. #include <stdio.h>
  3. #define R 50
  4. #define M(A)                \
  5. {                        \
  6. _Static_assert( A>=0 && A<=R, "in M(A) A out of range" ); \
  7.         s.a = A;        \
  8.         s.b = A+1;        \
  9.         s.c[A] = 0;        \
  10. }
  11. typedef struct {
  12.         int a;
  13.         int b;
  14.         int c[R+1];
  15. } s_t;
  16. s_t s;
  17. int main(int argc, char ** argv)
  18. {
  19.         int i=0;
  20.         M(51);
  21.         M(i);
  22.         return 0;
  23. }

  24. /* compile time error message:
  25. $ gcc -o s s.c
  26. s.c: In function ‘main’:
  27. s.c:20:2: error: static assertion failed: "in M(A) A out of range"
  28. s.c:21:2: error: expression in static assertion is not constant
  29. $
  30. */
復(fù)制代碼
2.一個不完美的數(shù)組下標(biāo)不能為負(fù)的方案,ANSI C實現(xiàn)。
文件:a.c

  1. /* ANSI C version */
  2. #include <stdio.h>
  3. #define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]
  4. #define R 50
  5. #define M(A)                \
  6. {                        \
  7. STATIC_ASSERT( A>=0 && A<=R, in_M_A_out_of_range ); \
  8.         s.a = A;        \
  9.         s.b = A+1;        \
  10.         s.c[A] = 0;        \
  11. }
  12. typedef struct {
  13.         int a;
  14.         int b;
  15.         int c[R+1];
  16. } s_t;
  17. s_t s;
  18. int main(int argc, char ** argv)
  19. {
  20.         int i=0;
  21.         M(51);
  22.         M(i);
  23.         return 0;
  24. }

  25. /* compile time error message:
  26. $ gcc -o a a.c
  27. a.c: In function ‘main’:
  28. a.c:21:1: error: size of array ‘static_assertion_in_M_A_out_of_range’ is negative
  29. $
  30. 不完美,變量i作參數(shù)沒有報錯,原因是C99支持

  31. Z:\>cl a.c
  32. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
  33. Copyright (C) Microsoft Corporation.  All rights reserved.

  34. a.c
  35. a.c(21) : error C2118: negative subscript
  36. a.c(22) : error C2057: expected constant expression
  37. a.c(22) : error C2466: cannot allocate an array of constant size 0

  38. Z:\>
  39. msvc由于不支持C99,反而能找到第二個問題。
  40. */
復(fù)制代碼
3. 利用gcc擴展的實現(xiàn)
文件:n.c

  1. /* GCC version */
  2. #include <stdio.h>
  3. #define CTC(X) ({ extern int __attribute__((error("assertion failure: '" #X "' not true"))) compile_time_check(); ((X)?0:compile_time_check()),0; })
  4. #define R 50
  5. #define M(A)                \
  6. {                        \
  7. CTC( A>=0 && A<=R ); \
  8.         s.a = A;        \
  9.         s.b = A+1;        \
  10.         s.c[A] = 0;        \
  11. }
  12. typedef struct {
  13.         int a;
  14.         int b;
  15.         int c[R+1];
  16. } s_t;
  17. s_t s;
  18. int main(int argc, char ** argv)
  19. {
  20.         int i=0;
  21.         M(51);
  22.         M(i);
  23.         return 0;
  24. }

  25. /* compile time error message:
  26. $ gcc -o n n.c
  27. n.c: In function ‘main’:
  28. n.c:21:2: error: call to ‘compile_time_check’ declared with attribute error: assertion failure: 'i>=0 && i<=R' not true
  29. n.c:22:2: error: call to ‘compile_time_check’ declared with attribute error: assertion failure: 'i>=0 && i<=R' not true
  30. $
  31. */
復(fù)制代碼
4.利用位域的C實現(xiàn),但用了__COUNTER__

  1. /* ANSI C version 2 */
  2. #include <stdio.h>
  3. #define CTASTR2(pre,post) pre ## post
  4. #define CTASTR(pre,post) CTASTR2(pre,post)
  5. #define STATIC_ASSERT(cond,msg) \
  6.     typedef struct { int CTASTR(static_assertion_failed_,msg) : !!(cond); } \
  7.         CTASTR(static_assertion_failed_,__COUNTER__)
  8. #define R 50
  9. #define M(A)                \
  10. {                        \
  11. STATIC_ASSERT( A>=0 && A<=R, in_M_A_out_of_range ); \
  12.         s.a = A;        \
  13.         s.b = A+1;        \
  14.         s.c[A] = 0;        \
  15. }
  16. typedef struct {
  17.         int a;
  18.         int b;
  19.         int c[R+1];
  20. } s_t;
  21. s_t s;
  22. int main(int argc, char ** argv)
  23. {
  24.         int i=0;
  25.         M(51);
  26.         M(i);
  27.         return 0;
  28. }

  29. /* compile time error message:
  30. $ gcc -o b b.c
  31. b.c: In function ‘main’:
  32. b.c:25:2: error: zero width for bit-field ‘static_assertion_failed_in_M_A_out_of_range’
  33. b.c:26:2: error: bit-field ‘static_assertion_failed_in_M_A_out_of_range’ width not an integer constant
  34. $

  35. Z:\>cl b.c
  36. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
  37. Copyright (C) Microsoft Corporation.  All rights reserved.

  38. b.c
  39. b.c(25) : error C2149: 'static_assertion_failed_in_M_A_out_of_range' : named bit
  40. field cannot have zero width
  41. b.c(26) : error C2057: expected constant expression
  42. b.c(26) : error C2149: 'static_assertion_failed_in_M_A_out_of_range' : named bit
  43. field cannot have zero width

  44. Z:\>
  45. */
復(fù)制代碼

論壇徽章:
2
技術(shù)圖書徽章
日期:2013-09-04 15:21:51酉雞
日期:2013-11-01 21:20:20
2 [報告]
發(fā)表于 2012-02-01 12:35 |只看該作者
如果A是常量,那么MACRO_P(33);之類的就應(yīng)該改成MACRO_P(SOME_VALUE);這樣看代碼才有意義,常量的范圍也很容易控制,如果是庫調(diào)用,那么被調(diào)用者應(yīng)該動態(tài)防范溢出,而不能寄托于編譯判斷。

論壇徽章:
0
3 [報告]
發(fā)表于 2012-02-01 12:38 |只看該作者
回復(fù) 2# mirnshi


    我只是舉例子,用數(shù)字比用常量名更能說明問題,這些地方就別較真了吧。如果6小時沒有人能回答我就公布答案。

論壇徽章:
0
4 [報告]
發(fā)表于 2012-02-01 12:42 |只看該作者
這樣也要報錯嗎?
const int a = 3333;
MACRO_P(a);

論壇徽章:
0
5 [報告]
發(fā)表于 2012-02-01 12:47 |只看該作者
AD8018 發(fā)表于 2012-02-01 12:42
這樣也要報錯嗎?
const int a = 3333;
MACRO_P(a);

不允許這么填參數(shù),只能是數(shù)字常量,不能是常變量,如果這個問題也能在編譯時查出來當(dāng)然更好。

論壇徽章:
0
6 [報告]
發(fā)表于 2012-02-01 13:05 |只看該作者
沒本 發(fā)表于 2012-02-01 12:47
不允許這么填參數(shù),只能是數(shù)字常量,不能是常變量,如果這個問題也能在編譯時查出來當(dāng)然更好。


那不簡單,需要幾個數(shù)組,就填幾個數(shù)值.

下面的A允許的范圍為 0~2


  1. #define CONCAT(X,Y)        X##Y

  2. #define Out_of_range_0
  3. #define Out_of_range_1
  4. #define Out_of_range_2


  5. #define MACRO_P(A)                                \
  6. {                                                                \
  7.         CONCAT(Out_of_range_,A)                \
  8.         printf("%d\n", A);                        \
  9. }

  10. int main()
  11. {
  12.         MACRO_P(1)       
  13.         MACRO_P(2)
  14.         MACRO_P(0)
  15.         //MACRO_P(10)
  16. }
復(fù)制代碼

論壇徽章:
0
7 [報告]
發(fā)表于 2012-02-01 16:02 |只看該作者
回復(fù) 6# AD8018


    這個方法能用,要是數(shù)值范圍比較大就會不大靈活。應(yīng)該還有更好的方法。

論壇徽章:
0
8 [報告]
發(fā)表于 2012-02-01 16:11 |只看該作者
沒本 發(fā)表于 2012-02-01 16:02
回復(fù) 6# AD8018


我堅信沒有,要有也是寫多個宏的變種。

要不公布答案吧,如果我輸了,愿意免費贈送百元之內(nèi)圖書一本,請網(wǎng)購自選,發(fā)鏈接,快遞到府上。。。。

論壇徽章:
0
9 [報告]
發(fā)表于 2012-02-01 16:12 |只看該作者
禁書除外

論壇徽章:
0
10 [報告]
發(fā)表于 2012-02-01 16:19 |只看該作者
回復(fù) 8# AD8018


    圖書就算了,我積分也可不少。到時間再公布答案,而且不只一個,我查到的方法就有四五種。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP