2017-07-20 58 views
-2

我在使用X宏中的offsetof()時出現明顯錯誤。下面的代碼顯示了一個相當簡單的結構的例子,一個是明確定義的,另一個是使用X宏定義的。然後將兩個結構中每個結構域的偏移量打印到屏幕上。正如您在下面的註釋中看到的那樣,使用X宏定義的結構中的字段顯示不正確的偏移量。x宏打印錯誤offsetof()信息

我認爲這是一個打印問題。有人可以解釋一下這個謎團嗎?

#include <stdio.h> 
#include <stdlib.h> 

const char fmt[] = "%-5s 0x%04x(%05d)\n"; 

#define i(s, f) \ 
    #f, \ 
    offsetof(s, f), \ 
    offsetof(s, f) 

int main(void) 
{ 
    /********************************** 
    METHOD1 - CORRECTLY PRINTS 

     a  0x0000(00000) 
     b  0x0004(00004) 
     c  0x0008(00008) 
     d  0x0030(00048) 

    **********************************/ 
    typedef struct { 
     uint32_t a; 
     uint32_t b; 
     uint32_t c[10]; 
     uint32_t d; 
    } struct1; 

    printf(fmt, i(struct1, a)); 
    printf(fmt, i(struct1, b)); 
    printf(fmt, i(struct1, c)); 
    printf(fmt, i(struct1, d)); 

    printf("\n"); 

    /********************************** 
    METHOD2 - PRINTS WRONG INFO 

     a  0x0000(00000) 
     b  0x0004(00004) 
     c[10] 0x0030(00048) <== WRONG 
     d  0x0030(00048) 

    **********************************/ 

    #define FIELDS \ 
     X(uint32_t, a,  "") \ 
     X(uint32_t, b,  "") \ 
     X(uint32_t, c[10], "") \ 
     X(uint32_t, d,  "") \ 

    typedef struct { 
    #define X(type, name, descr) type name; 
     FIELDS 
    #undef X 
    } struct2; 

    #define X(type, name, descr) printf(fmt, i(struct2, name)); 
     FIELDS 
    #undef X 

    return 0; 
} 
+0

您使用錯誤的格式說明符,導致未定義的行爲。 '%x'用於unsigned int,'%d'用於'int',但'offsetof'則產生'size_t'。 –

+0

你的X-macro最終輸出了'offsetof(struct2,c [10])',這與第一個例子不同,它的結果是'offsetof(struct1,c)' –

+0

謝謝@MM澄清問題發生的位置。你能想出一種方法來重新定義我的x宏以產生所需的結果嗎? –

回答

3

在第二X宏:

#define X(type, name, descr) printf(fmt, i(struct2, name)); 
    FIELDS 

此擴展爲:

printf(fmt, "a", offsetof(struct2, a), offsetof(struct2, a)); 
printf(fmt, "b", offsetof(struct2, b), offsetof(struct2, b)); 
printf(fmt, "c[10]", offsetof(struct2, c[10]), offsetof(struct2, c[10])); 
printf(fmt, "d", offsetof(struct2, d), offsetof(struct2, d)); 

offsetof(struct2, c[10])顯然是不一樣offsetof(struct2, c)

-1

在上述評論的幫助下,我想出了答案。對於數組的結構元素,我需要有一個不同的X宏定義。

#include <stdio.h> 
#include <stdlib.h> 

const char fmt[] = "%-5s 0x%04x(%05d)\n"; 

#define i(s, f) \ 
    #f, \ 
    offsetof(s, f), \ 
    offsetof(s, f) 

int main(void) 
{ 
    /********************************** 
     METHOD1 - CORRECTLY PRINTS 

      a  0x0000(00000) 
      b  0x0004(00004) 
      c  0x0008(00008) 
      d  0x0030(00048) 

    **********************************/ 
    typedef struct { 
     uint32_t a; 
     uint32_t b; 
     uint32_t c[10]; 
     uint32_t d; 
    } struct1; 

    printf(fmt, i(struct1, a)); 
    printf(fmt, i(struct1, b)); 
    printf(fmt, i(struct1, c)); 
    printf(fmt, i(struct1, d)); 

    printf("\n"); 

    /********************************** 
     METHOD2 - PRINTS WRONG INFO 

      a  0x0000(00000) 
      b  0x0004(00004) 
      c  0x0008(00008) <== CORRECT 
      d  0x0030(00048) 

    **********************************/ 

    #define FIELDS \ 
     X(uint32_t, a,  "") \ 
     X(uint32_t, b,  "") \ 
     AX(uint32_t, c, 10, "") \ 
     X(uint32_t, d,  "") \ 

    typedef struct { 
    #define X(type, name, descr)  type name; 
    #define AX(type, name, size, descr) type name[size]; 
     FIELDS 
    #undef X 
    #undef AX 
    } struct2; 

    #define X(type, name, descr)  printf(fmt, i(struct2, name)); 
    #define AX(type, name, size, descr) printf(fmt, i(struct2, name)); 
     FIELDS 
    #undef X 
    #undef AX 

    return 0; 
}