Re: struct type completion
- From: Lawrence Kirby <lknews@xxxxxxxxxxxxxxx>
- Date: Mon, 18 Apr 2005 17:19:18 +0100
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
.
- Follow-Ups:
- Re: struct type completion
- From: S.Tobias
- Re: struct type completion
- References:
- struct type completion
- From: S.Tobias
- struct type completion
- Prev by Date: Re: char*
- Next by Date: Re: Convert float to string without trailing 0s
- Previous by thread: struct type completion
- Next by thread: Re: struct type completion
- Index(es):
Relevant Pages
|