Undocumented Gcc Extension: Vla in Struct

Undocumented GCC Extension: VLA in struct

See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37428

and also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42121

Yes, it's weird.

Variable length array in the middle of struct - why this C code is valid for gcc

GCC does not allow it, compile with -std=c99 -pedantic-errors. A VLA inside a struct is apparently a (poorly documented) non-standard GNU C feature. See this.

Array of variable length in a structure

i would suggest an alternative like this:

typedef struct 
{
int width;
int height;
struct pixel *pixels;
} image;

every time, you need to alloc:

image img;
/* init */
img.pixels=malloc(sizeof(struct pixel)*img.width*img.height);

*(img.pixels+img.height*i+j) represents img.pixels[i][j].

Undocumented GCC C++11 extension? Capturing arbitrary expressions in lambda capture lists

As noted in the comments, the feature is broadly similar to a recent proposal but it was implemented long before initial standardization. GCC served as a prototype during the standard development and initially reflected whatever ideas the authors liked, which were later refined. Some ideas which had to be trimmed to keep the standard reasonably simple are getting reintroduced as proposals. Lambdas have a lot of room to grow.

For now, this is just another bug. It was never removed from the original implementation because nobody has reported it yet.


Update: This is now a standard feature since C++14.

C initialize array within structure

You can make that work in gcc by making the struct either static or global, but it turns out that initializing flexible array members is non-conforming and so it is likely to not work except with gcc. Here is a way to do it that just uses C99-conforming features...

#include <stdlib.h>
#include <stdarg.h>

typedef struct Grid {
int rows;
int cols;
int grid[];
} *Grid;

Grid newGrid(int, int, ...);

Grid newGrid(int rows, int cols, ...)
{
Grid g;
va_list ap;
int i, n = rows * cols;

if((g = malloc(sizeof(struct Grid) + rows * cols * sizeof(int))) == NULL)
return NULL;
g->rows = rows;
g->cols = cols;
va_start(ap, cols);
for(i = 0; i < n; ++i)
g->grid[i] = va_arg(ap, int);
va_end(ap);
return g;
}
.
.
.
Grid g1, g2, g3;
g1 = newGrid(1, 1, 123);
g2 = newGrid(2, 3, 1, 1, 1,
2, 2, 2);
g3 = newGrid(4, 5, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20);


Related Topics



Leave a reply



Submit