Important points you should remember before using flexible array member
I have already written an article on flexible array member and their importance with some example code. This blog post explains some important points that you should remember before using the flexible array member in your code.
If you don’t know about the flexible array member, then it is my recommendation you should read first about the flexible array member. It helps you to understand the article otherwise you will face difficulty in understanding.
1. Incomplete array must be the last element of the structure otherwise you will get a compile-time error.
struct s
{
int n;
double d[];
};
Correct declaration of a flexible array member
struct s
{
int n;
double d[];
int m;
};
Wrong declaration of a flexible array member
2. structure must have at least one named member with an incomplete array type.
struct s
{
int n; //named member
double d[];
};
Correct declaration of a flexible array member
struct s
{
double d[];
};
Wrong declaration of a flexible array member
3. 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 a lack of information about its members.
4. In particular, the size of the structure is as if the flexible array member was omitted except that it may have more trailing padding than the omission would imply. for detail, you can see the article, flexible array member in C.
5. let’s assume struct s is a structure that has a flexible array member.
struct s
{
int n;
double d[];
};
Now let’s see the common issue while declaring the object using the struct s. here I am declaring two-variable s1 and s2.
7. If sizeof (double) is 8, then after the following code is executed:
struct s
{
int n;
double d[];
};
int main()
{
struct s *s1;
struct s *s2;
s1 = malloc(sizeof (struct s) + 64);
s2 = malloc(sizeof (struct s) + 46);
return 0;
}
I am assuming that the calls to malloc succeed, the objects pointed to by s1 and s2 behave, for most purposes, as if the identifiers had been declared as:
struct
{
int n;
double d[8];
} *s1;
struct
{
int n;
double d[5];
} *s2;
8. If sizeof (double) is 8, then after the following code is executed:
#include
struct s
{
int n;
double d[];
};
int main()
{
struct s *s1;
struct s *s2;
s1 = malloc(sizeof (struct s) + 10);
s2 = malloc(sizeof (struct s) + 6);
return 0;
}
I am assuming that the calls to malloc succeed, the objects pointed to by s1 and s2 behave, for most purposes, as if the identifiers had been declared as:
struct
{
int n;
double d[1];
} *s1, *s2;
Now I try to access the array d with a double-pointer.
9. If writing below expression (assignment s2 to s1).
*s1 = *s2;
only copies the member n. If any of the array elements are within the first sizeof (struct s) bytes of the structure, they might be copied or simply overwritten with indeterminate values.
Here I have tried to cover almost all Important points you should remember before using flexible array member. Still, if you have any queries or suggestions you can write in the comment box or you can email me directly.