Possible Duplicate:
Is the “struct hack” technically undefined behavior?
I checked if zero length arrays were allowed in C++11. It appeared they aren't. From 8.3.4 Arrays [dcl.array]
If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.
Since i cant use zero length arrays Is it possible to use variable length structs while being standard/Well Defined? For example I'd want to do something like the below. How do I make it well defined and standard when the buffer MAY BE EMPTY.
-edit- related: Array of zero length
struct MyStruct {
uint size;
int32 buf[0];//<-- NonStandard!
};
...
auto len=GetLength();
auto ptr=GetPtr();
auto bytelen=len*sizeof(int32);
var p = reinterpret_cast<MyStruct*>(malloc(bytelen))
p->size=len
memcpy(p->buf, ptr, bytelen)
return p;