문제

I have an odd problem with gcc 4.3 and I wanted to know if it is a specific problem with the compiler or if it is a general C problem. Granted, I use a really odd construct, but I like it, because it allows me to enforce some rules that otherwise would not be possible. The project is split in several modules and each module has a structure that is opaque. There's a typedef struct <tag> <type> declaration in the header and in 1 c file, there is a struct tag { ... }; and all function refer to an element via a <type> *.

Each module knows its own structure, the structure of the other modules are not visible. In one module, I do not work with 1 element, but with a fixed array of elements. This means that some functions of that module work with a pointer to array. Let's call that module wdi. So I have for example

void write_all(wdi_type (*wdis)[MAX_WDI]);

and for allocation (I know very unusual syntax) to return directly the right pointer to an array.

wdi_type (*wdi_alloc(void))[MAX_WDI];

This works well under GNU-C 3.4.6 (Solaris SPARC), under cc, the sun compiler v12 it compiles also (couldn't test it though because another part of the app breaks). On gcc 4.3.3 (also tested on 4.4.6 x86-64 and 4.6.2 ARM) though, it doesn't. I get the compilation error array type has incomplete element type. I don't understand why the compiler would need that information at that stage. It doesn't need the size of other opaque structures either.

Is it a gcc bug?

What does the standard say?

I wasn't able to find something about it. Should I file a bug report to GNU?

도움이 되었습니까?

해결책

The standard (well, the N1570 draft of the C2011 standard) says, in 6.2.5 (20)

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified.

(emphasis by me)

The corresponding passage in the C99 standard was less forceful:

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type.36)

36) Since object types do not include incomplete types, an array of incomplete type cannot be constructed.

it didn't explicitly forbid specifying an array type for an incomplete element type, only constructing such an array.

I haven't been able to find out when and why footnote 36 was replaced with the emphasised sentence, but it was before November 2010.

It would seem that gcc-4.x rejects the code based on the new version, while gcc-3.4.6 accepted it based on the older version, so I don't think it is a bug, and the code is explicitly invalid according to the current standard.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top