c - Is it legal to create a flexible array member of size zero? -
the c99 standard allows creation of flexible array members such as
typedef struct pstring { size_t length; char string[]; } pstring; this initialized pstring* s = malloc(sizeof(pstring) + len). permissible len zero? seem consistent, , nice saving space time time (probably not pstring example, of course). on other hand, have no idea following code do:
pstring* s = malloc(sizeof(pstring)); s->string; this seems sort of thing might work 1 compiler , not another, or on 1 os , not another, or on 1 day , not another, want know standard says this. malloc in example code undefined behavior, or access s->string invalid, or else entirely?
what valid, accessing s->string[0] or feeding s->string function accessing data invalid.
the c99 standard says (§6.7.2.1):
struct s { int n; double d[]; };...
struct s t1 = { 0 }; // valid...
the assignment
t1.d[0]undeļ¬ned behavior, possible that
sizeof (struct s) >= offsetof(struct s, d) + sizeof (double)in case assignment legitimate. nevertheless, cannot appear in strictly conforming code.
Comments
Post a Comment