What are the differences between Variable Length Arrays and Flexible Array Member?
Asked Answered
G

1

5

I've seen in the ISO C99 committee draft that structs can have an incomplete an array with unspecified size its end, known as Flexible Array Member.

On the other hand C99 also has Variable Length Arrays, which allow declaring arrays with size not constant at compile-time.

I thought that a FAM was a special kind of a VLA, but I've seen two SO users claiming otherwise. Also, reading the Wikipedia section on sizeof, it says that sizeof behaves differently for those two.

Why do both of them exist instead of just one? (Are their use-cases too different?)

Also, which other associated behaviors are different for each of them?

Georginegeorglana answered 29/11, 2015 at 2:30 Comment(1)
Wikipedia is correct about sizeof. The size of a VLA is a run-time computed value — unlike all other occurrences of sizeof. The size of a structure containing a FAM is fixed at compile time and does not include the size of the FAM.Mannie
B
7

There are two different things that the C99 standard added and they are easy to mix up by mistake:

Flexible array members. This means that a struct can have a member of unknown size at the end. Example from the C standard:

    struct s { int n; double d[]; };

int m = /* some value */;
struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));

This was used before C99 as well, but it was then undefined behavior, known as the "struct hack" referred to in another answer. Before C90, there could be unexpected padding bytes at the end of the struct, leading to bugs.

Variable length arrays (VLA). These are arrays with their size set in runtime. They are most likely implemented by the compiler by using dynamic memory allocation. Example:

void func (int n)
{
  int array[n];
}

referred from user29079 : https://softwareengineering.stackexchange.com/questions/154089/c-flexible-arrays-when-did-they-become-part-of-the-standard

Balch answered 29/11, 2015 at 2:43 Comment(4)
But why would anyone use the first instead of the second? Is it impossible to use VLA as struct members?Georginegeorglana
@VillasV: Yes, it is impossible to use a VLA in a structure. The size of a structure has to be known at compile time. FAMs are not counted in the size of the structure, though they may affect the amount of padding in the structure.Mannie
Flexible array members don't represent a new feature so much as they represent a partial easing of a restriction which should never have existed in the first place. Before C89, many compilers would treat an array declaration with a size of zero as creating a pointer to the spot where the first array element would go, but without allocating space for the array, which is how compilers will typically treat flexible array members. Compilers are allowed to treat flexible array members in slightly different ways, but I don't think those would have evolved if the Standard had simply allowed...Summerly
...zero-size array declarations from the start.Summerly

© 2022 - 2024 — McMap. All rights reserved.