2013-01-16 3 views
2

SDCC에 문제가 있습니다. 내 코드 (다른 컴파일러에서 이식하려고 시도 중)는 유연한 배열 멤버가있는 구조체를 사용합니다. 그러나, 나는 다음과 같은 코드를 컴파일하려고 : 무엇 제공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 

:

/** 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 다음과 같은 오류를 준다? 이 코드는 gcc에서 잘 컴파일됩니다. 사용하고있는 다른 z80 컴파일러는 말할 것도 없습니다.

편집 : 관련된 것으로 보이는 this SDCC bug이 발견되었습니다. 누군가가 그것이 무엇인지 그리고 어떻게 설명 할 수 있습니까?

답변

3

SDCC가 바로 거기에 있으며, gcc-4.6.2에서는 "괜찮습니다"라고 컴파일하지 않습니다. 음, 만약 당신이 그것을 pedantically 표준을 준수하도록 요청하십시오. -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의 구성원 또는 배열로 배열가요 멤버와 struct S를 갖는 수 (강조 내이다). 표준은 이것을 금지하므로이를 사용하는 코드는 이식성이 없으며 모든 컴파일러는 코드를 거부 할 권리가 있습니다.

링크 된 문제는 관련이 없습니다. 유연한 배열 구성원이있는 struct이 자동으로 인스턴스가 만들어지면 경고가 표시되지 않습니다. 표준에 따라 허용되지 않지만 (SDCC 및 기타 확장 프로그램에서 허용됩니다.)).

+0

우수. SDCC 문서는 좋지 않아서 GCC의 성명서를 수락하면 더 혼란스러워했습니다. 고마워요! – thirtythreeforty

1

을 생각해보십시오. 이것은 오류입니다.

str []이 가변 길이이므로 STRLIST_ENTRY의 크기가 알 수 없습니다.

STRLIST에는 STRLIST_ENTRY의 가변 길이 배열이 포함되어 있습니다.

메모리에서 배열은 STRLIST_ENTRY의 시퀀스로 서로 바로 배치됩니다.

그 요소는 알 수없는 크기이므로 인덱스가 가리키는 오프셋을 어떻게 알 수 있습니까?

char str []는 고정 된 크기를 지정하거나 배열 외부의 문자열에 char *을 지정해야합니다.