views:

118

answers:

4

What is a Flexible Array, exactly? I can't find much on it.

+2  A: 

There isn't such thing as a flexible array, but there is such thing as a flexible array member. It is used to provide access to variable-length data as a member of a struct.

From http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/strct.htm:

A flexible array member, which is a C99 feature, can be an element of a structure with more than one named member. A flexible array member can be used to access a variable-length object. The flexible array member must be the last element of such a structure, and it has incomplete type.

danben
A: 

Does this describe what you are asking about? This is not unique to Sun's C compiler, but it was the first clear overview I found:

http://docs.sun.com/app/docs/doc/820-7598/6nirjuni7?a=view

Dave Costa
+5  A: 

There are a couple of things it could refer to. The only place I've seen that precise wording used much is C99 though.

A flexible array member is the official C99 name for what used to (usually) be called the "struct hack". The basic idea is that you define a struct something like this:

struct x {
    int a; // whatever members you want here.
    size_t size;
    int x[]; // no size, last member only
};

This is used primarily (or exclusively) with dynamic allocation. When you want to allocate an object of this type, you allocate enough extra space for whatever size of array you need:

struct x *a = malloc(sizeof(struct x) + 20 * sizeof(int));
a->size = 20;

The size member isn't strictly necessary, but often handy to keep track of the size allocated for a item. The one above has space for 20 int's, but the main point of this is that you might have several around, each with its own size.

Jerry Coffin
Should probably hide the complex `malloc()` call behind a function: `struct x *struct_x_alloc(size_t s) { struct x *t = malloc(sizeof t + s * sizeof(int)); t->size = s; return t; }`
Chris Lutz
@Chris: yes, but don't go too far toward making it all manageable, or people will start to mistake it for C++. ;-)
Jerry Coffin
@Chris: your size calculation is wrong - it should read `sizeof *t + ...`; instead of `sizeof(int)`, you can also use `sizeof *t->x`
Christoph
@Christoph - My bad, but it's too late to edit. Yours is wrong also: `sizeof t->x` (I wasn't sure if a dereference was allowed for `sizeof` but I believe it is. Either way, an array size variable _better_ be `size_t`)
Chris Lutz
+1  A: 

A flexible array is an array whose index bounds are determined at run time and may change during the lifetime of the array.

Java arrays are flexible. For example, in Java you can assign one array to another:

int[] a1 = {1, 2, 3, 4};
int[] a2 = {1, 2, 3};
a1 = a2;

At first, a1 has index range 0–3, and a2 has index range 0–2.

After the assignment a1 = a2, a1 points to an array with index range 0–2, so the index range of a1 varied during the lifetime of a1.

Joe Suarez