Does the above code lead to Undefined Behavior when used in C++? According to [ISO C++] flexible array members are not allowed, however the code appears to be a workaround for this limitation. Is it safe to use?
struct Foo {
size_t len;
size_t cap;
char* buf() { return reinterpret_cast<char*>(this + 1); }
};
Foo* new_foo(size_t sz) {
Foo* foo = reinterpret_cast<Foo*>(malloc(sz + sizeof(Foo)));
foo->len = 0;
foo->cap = sz;
return foo;
}