Flexible array members can lead to undefined behavior?

The Short Answer Yes. Common conventions of using FAMs expose our programs to the possibility of undefined behavior. Having said that, I’m unaware of any existing conforming implementation that would misbehave. Possible, but unlikely. Even if we don’t actually reach undefined behavior, we are still likely to fail strict conformance. No. The offset of the … Read more

How to initialize a structure with flexible array member

No, flexible arrays must always be allocated manually. But you may use calloc to initialize the flexible part and a compound literal to initialize the fixed part. I’d wrap that in an allocation inline function like this: typedef struct person { unsigned age; char sex; size_t size; char name[]; } person; inline person* alloc_person(int a, … Read more

Array of zero length

Yes this is a C-Hack. To create an array of any length: struct someData* mallocSomeData(int size) { struct someData* result = (struct someData*)malloc(sizeof(struct someData) + size * sizeof(BYTE)); if (result) { result->nData = size; } return result; } Now you have an object of someData with an array of a specified length.

Is using flexible array members in C bad practice?

It is an accepted “fact” that using goto is poor software engineering practice. That doesn’t make it true. There are times when goto is useful, particularly when handling cleanup and when porting from assembler. Flexible array members strike me as having one main use, off the top of my head, which is mapping legacy data … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)