I've been trying out using offsetof to calculate the size of structures with flexible array members, and CompCert appears to be the only compiler that fails this test case (where flexible array members are supported at all). I'm not sure if it should fail, but you might want to know.
Note that I'm not 100% whether this code is supposed to work according to the standard.
Test case code
#include<stddef.h>#include<stdlib.h>typedefstruct {
inta;
intb;
doublec;
doubled;
} Element;
typedefstruct {
size_tused;
size_tcapacity;
Elementitems[]; /* Flexible array member. */
} Container;
Container*bar(
unsignedsomeNumber)
{
returnmalloc(offsetof(Container, items[someNumber]));
/* Other initialisation omitted. */
}Expected outcome
I'd expect to get a function that allocates memory of the stack where the size of the FAM is large enough to take someNumber elements.
What I got
(Running on godbolt with version 3.12)
<source>:20: error: array element designator is not an integer constant expression
1 error detected.
I've been trying out using
offsetofto calculate the size of structures with flexible array members, and CompCert appears to be the only compiler that fails this test case (where flexible array members are supported at all). I'm not sure if it should fail, but you might want to know.Note that I'm not 100% whether this code is supposed to work according to the standard.
Test case code
Expected outcome
I'd expect to get a function that allocates memory of the stack where the size of the FAM is large enough to take
someNumberelements.What I got
(Running on godbolt with version 3.12)