Skip to content

Support for gnu extension flexible array initializer #264

Open

Description

@ubitux

Example code:

#include <stdio.h>

struct int_def {
    const char *s;
    const int ints[];
};

static const struct int_def my_ints = {
    .s = "my ints",
    .ints = {0, 3, 7, 2, -1},
};

int main() {
    const struct int_def *def = &my_ints;
    const int *v = def->ints;

    printf("%s\n", def->s);
    while (*v != -1) {
        printf("%d\n", *v);
        v++;
    }
    return 0;
}

This is a common idiom used at the end of structures, making their size variable. This is the expected output on GCC or Clang:

my ints
0
3
7
2

With CompCert 3.4, it doesn't compile: error: initializer element is not a compile-time constant (wrong number of elements in array initializer)

Activity

  1. bschommer commented on Jan 28, 2019

    @bschommer
    Member

    This is actually not standard C99 but rather a gnu extension. You also get a warning if you compile it with -pedantic and -std=c99 for both gcc and clang.

  2. changed the title [-]Support for extensible structures ending with empty-length arrays[/-] [+]Support for gnu extension flexible array initializer[/+] on Jan 28, 2019
  3. bschommer commented on Jan 28, 2019

    @bschommer
    Member

    I changed the title to reflect the real problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions