On Mon, 10 Nov 2025, Qing Zhao wrote:
> > struct { int a; char b[] __attribute__ ((counted_by (a))); } *x;
>
> For such case, usually how to initialize the above x?
> Since the anonymous structure has a FAM field, usually we need to use malloc
> to initialize it, is this correct?
> Or is there other way to initialize such variable?
Yes, use malloc
> If we have to use malloc to initialize, how to do it? (There is no structure
> name, how can we get the size of
> the structure?) one way to do it is to manually calculate the size of the
> anonymous structure with use
> malloc to initialize.
The type can be accessed as typeof (*x).
> > is a legitimate use of an unnamed structure type, where the counted_by
> > checks should take place at the level of that type rather than expecting
> > it to appear in a larger type. Similarly, if you name the field using a
> > type without a tag,
> >
> > struct s { struct { int a; char b[] __attribute__ ((counted_by (a))); } x;
> > };
>
> Again, for this case, how to initialize a variable with “stuct s”?
If you need the type of the member x, again, it can be accessed with
typeof.
> > This means the patch should include testcases (both valid, and invalid
> > ones being diagnosed) with unnamed structure or union types used in
> > contexts that *don't* insert their members into the namespace of some
> > outer structure or union, and verify that the compiler behaves as expected
> > on such cases (and I suspect the code based on TYPE_NAME will need
> > changing to make it behave correctly).
>
> Does the above two cases you listed cover all such cases?
There are the various extensions (-fms-extensions / -fplan9-extensions) to
consider as well, but it might be reasonable to say that only genuinely
anonymous struct / union members get the special counted_by handling, not
members with no name but using a previously named type (only in the
genuinely anonymous case is there no way to access the anonymous type of
the anonymous member directly).
--
Joseph S. Myers
[email protected]