Re: struct type completion



On Mon, 18 Apr 2005 15:12:46 +0000, S.Tobias wrote:

> I'm trying to understand how structure type completion works.
>
> # A structure or union type of unknown
> # content (as described in 6.7.2.3) is an incomplete type. It
> # is completed, for all declarations of that type, by
> ^^^
> # declaring the same structure or union tag with its defining
> # content later in the same scope.
> ^^^^^
> (6.2.5#23)
>
> # [#3] All declarations of structure, union, or enumerated
> ^^^
> # types that have the same scope and use the same tag declare
> ^^^^^^^^^^^^
> # the same type. The type is incomplete100) until the
> ^^^^^^^^^^^^^^
> # closing brace of the list defining the content, and complete
> # thereafter.
> (6.7.2.3#3)
> (both excerpts are from n869.txt, but they're the same in the Std)
>
> So (eg. in a file scope):
>
> /* 1 */ struct mystruct object;
> /* 2 */ struct mystruct { /*...*/ };
>
> line 1 defines `object' (although at this point the body of `struct
> mystruct' is not yet known(?)); and line 1 does not work without
> line 2.

Line 1 is invalid. When you define an object the type must be complete at
the point of definition, it cannot be completed later. Note that you can
define a pointer to an incomplete type whch is completed later. All
pointer types are object types, even pointers to incomplete or function
types.

> What I don't understand is, that the excerpts above seem to dictate that
> line 2 fully defines the type for all instances of `struct mystruct'.
> The type in line 1 must be a complete type; if it were not, then it
> would not be the same type as in line 2, which seems to be required by
> the second quote; and - what's obvious - we could not define `object'
> (we can't define an object with incomplete type, can we?).

At the point that line 1 is translated its type is incomplete. For any
code after line 2 it would appear complete (except of course that it is
invalid). So taking a valid example

/* 1 */ struct mystruct *ptr;

/* sizeof *ptr is invalid here */

/* 2 */ struct mystruct { /*...*/ };

/* sizeof *ptr is valid here because it is equivalent to
sizeof(struct mystruct) which is now complete */

> But then further description seems to contradict this: "... incomplete
> until the closing brace ...". If the type in line 1 is complete, then
> it needn't be completed, and can't of course be incomplete at the same
> time.

A type can be incomplete at one point in the source file and then complete
later on.

Lawrence
.



Relevant Pages

  • Re: struct type completion
    ... >> # content later in the same scope. ... > define a pointer to an incomplete type whch is completed later. ... the scopes of the declarations are different ...
    (comp.lang.c)
  • Re: B0
    ... if you've got an assembler like MASM, TASM, or HLA that supports ... declarations, this union declaration makes it very clear that my_va* ... programmer rather than a great programmer. ...
    (alt.lang.asm)
  • Re: null function pointer?
    ... >>> branch or the pointer to incomplete type branch? ... After `struct foo' definition `struct foo' type becomes ... All declarations of structure, union, or enumerated types that ...
    (comp.lang.c)
  • Re: null function pointer?
    ... >> The type is incomplete until the closing brace of the list ... > point of the code in your example in all declarations `struct foo' ... > declarations have the same scope and use the same struct tag). ... > type before and after the struct definition, ...
    (comp.lang.c)
  • Re: DeviceIoControl
    ... > I searched for it all over the Internet, ... > declarations for Delphi. ... > CTL_CODE and the UNION clause). ...
    (borland.public.delphi.nativeapi)