2013-01-16 36 views
2

我有一個SDCC的問題。我的代碼(我試圖從另一個編譯器中移植)使用具有靈活數組成員的結構。然而,當我嘗試編譯下面的代碼:sdcc不接受代碼

/** header of string list */ 
typedef struct { 
    int nCount; 
    int nMemUsed; 
    int nMemAvail; 
} STRLIST_HEADER; 

/** string list entry data type */ 
typedef struct { 
    int nLen; 
    char str[]; 
} STRLIST_ENTRY; 

/** string list data type */ 
typedef struct { 
    STRLIST_HEADER header; 
    STRLIST_ENTRY entry[]; 
} STRLIST;      // By the way, this is the line the error refers to. 

int main() 
{ 
    return 0; 
} 

SDCC提供了以下錯誤:

$ sdcc -mz80 -S --std-c99 test.c 
test.c:18: warning 186: invalid use of structure with flexible array member 
test.c:18: error 200: field 'entry' has incomplete type 

是怎麼回事?這段代碼在gcc中編譯得很好,更不用說我使用的其他z80編譯器了。

編輯:我發現this SDCC bug這似乎是相關的。有人可以解釋它是否是和如何?

回答

3

SDCC就在那裏,gcc-4.6.2也沒有編譯它「很好」。那麼,如果你要求它遵循標準的迂迴。

-std=c99 -pedantic(或-std=c1x -pedantic)編譯,GCC發射

warning: invalid use of structure with flexible array member 

和鐺-3.0的行爲類似,其警告是略微更多的信息:

warning: 'STRLIST_ENTRY' may not be used as an array element due to flexible array member 

的標準禁止,在6.7 .2.1(3):

A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

(重點是礦)

gcc和鐺允許具有struct s的柔性陣列成員爲struct小號成員或陣列作爲一個擴展。該標準禁止使用該標準,因此使用該標準的代碼不可移植,並且每個編譯器都有權拒絕該代碼。

鏈接的問題是不相關的,如果具有靈活數組成員的struct被實例化爲自動的,這不是每個標準都允許的(但被SDCC和其他人接受作爲擴展)。

+0

非常好。 SDCC文檔最多是窮人,而GCC接受該聲明有助於進一步混淆我。非常感謝你! – thirtythreeforty

1

想想爲什麼這是一個錯誤。

STRLIST_ENTRY的大小未知,因爲str []是可變長度。

STRLIST包含一個可變長度的STRLIST_ENTRY數組。

在內存中,數組是一系列緊跟在一起的STRLIST_ENTRY。

由於這些元素的大小未知,因此如何才能知道索引指向的偏移量?

char str []應該給定一個固定的大小,或者將一個char *作爲數組之外的字符串。