What is flexible array member in c?

flexible array member in c

A very good feature that is introduced by C99 is flexible array member. This feature enables the user to create an empty array in a structure, the size of the empty array can be changed at runtime as per the user requirements. This empty array should be declared as the last member of the structure and the structure must contain at least one more named member.

When a structure has a flexible array member, the entire structure becomes an incomplete type. Basically, an incomplete type structure is a type that has lack of information about its member.

For more detail see this link: All about array in c

There is some example of incomplete type

  • An array type whose dimension is not specified.
  • A structure type whose members not specified completely.
  • A union type whose members not specified completely.




An example of flexible array in c

In C99, you can use a structure whose last member is an array with an unspecified dimension, see the below structure.

Size of the structure who has flexible array member

Let’s consider the below structure as an example to calculate the size.

When we calculating the size of the above structure then we found that size of the structure includes all the member’s size including the padding bytes (if required) but not including the size of the flexible array member.

For example, if the size of an int and float is 4 bytes and the alignment of the sEmployInfo is also 4 bytes, then the compiler will not insert any padding bytes after the members iEmpId and fSalary. So when we calculate the size of the structure, then it will be 8 bytes.

sizeof(sEmployInfo) = sizeof(iEmpId)+ sizeof(fSalary);

Sample program to calculate the size of the structure

In below example code I am calculating the size of the array which contains flexible array member.





Why is a flexible array member required?

To understand the above question we need to take an example. Suppose there is an application which has a structure which contains the character array whose size is 30 bytes. This array is used to store the address of the users. Here, problem is that when you create a variable using this structure then every time compiler reserve 30 bytes for the array.

If the length of the user address is less than 30 bytes, the extra remaining memory of array would be a waste.In some scenario, it can be possible the length of the address could be greater than 30 bytes, this type of situation creates a problem and might be you will get the boundary issues.

See the below programs for the better understanding

In below program employee address is less than 30 bytes so here memory is wasting.

In below program employee address is greater than 30 bytes so here we can faced core dumping/segmentation fault.

c flexible array

So to resolve the above problem we need a dynamic array, previously many people use struct hack but later C99 introduce the concept of flexible array member in C language.Flexible array members enable you to create a dynamic size array as per the requirements at run-time.



How to use flexible array member in c

In below program, I have created a flexible array in the structure in place of the static array whose size is fixed( 30 bytes). The size of the flexible array change at run time. In the example code, I have created two variables which can store the address less than 30 and greater than 30 bytes. 

array flexible in c




2 comments

  1. If you are allowed to use malloc(), then no need of all this circus.
    Just define a pointer of the type you need and do malloc() or realloc().
    That would do the same thing.
    How is your approach better than this?

    1. When you create a pointer in structure then it requires extra bytes of memory which increase the size of the structure and you have to explicitly allocate the memory for the pointer and separately need to free the allocated memory.

Leave a Reply