[cfe-dev] Flexible array members in ObjC classes

David Chisnall via cfe-dev cfe-dev at lists.llvm.org
Thu Sep 21 02:14:42 PDT 2017


On 21 Sep 2017, at 10:07, Nat! via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> My second reaction, was to think that there are class cluster classes with allocate a flexible amount of memory (say string classes) that aren't ususally subclassed. Who am I to say, that using a convenient flexible array there can't be done (anymore) ?

This is more or less how a number of the GNUstep Foundation classes work, using the extra data allocated with class_createInstance() and doing some pointer arithmetic.  Flexible array members would possibly simplify the code, but they’re not needed for this to work.

> I would think a warning would be nice for classes who try to subclass a class with a flexible array ivar if they add another property or ivar.

The problem is that you can’t do this with the non-fragile ABI.  You can write the flexible array member in the @implementation of a class and the person writing the subclass won’t see it.

To make this safe, the minimum set of constraints that you’d need would be:

 - The ivar is the last one
 - The ivar is declared in the @interface
 - The class does not declare any ivars in the @implementation
 - Subclassing a class whose @interface ends with a flexible member is an error

With this set of constraints, flexible array members would be no less safe than using them in C.

David




More information about the cfe-dev mailing list