Skip to content

Edge case with offsetof #549

@dkfellows

Description

@dkfellows

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>

typedef struct {
    int a;
    int b;
    double c;
    double d;
} Element;

typedef struct {
    size_t used;
    size_t capacity;
    Element items[]; /* Flexible array member. */
} Container;

Container* bar(
    unsigned someNumber)
{
    return malloc(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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions