This is C++, not C. You don't need this flexible array member hack in C++, because you can easily make a template class which can endow any struct with a flexible array past the end and encapsulate the pointer arithmetic calculation and the memory allocation to make it work. Watch:
#include
template class flex_struct {
public:
TYPE *tail()
{
return (TYPE *) ((char *) this + padded_size());
}
// substitute malloc/free here for new[]/delete[] if you want
void *operator new(size_t size, size_t tail)
{
size_t total = padded_size() + sizeof (TYPE) * tail;
return new char[total];
}
void operator delete(void *mem)
{
delete [] (char *) mem;
}
private:
static size_t padded_size() {
size_t padded = sizeof (flex_struct);
if(padded % alignof(TYPE) != 0) {
padded = padded & ~(alignof(TYPE)-1) + alignof(TYPE);
}
return padded;
}
};
struct mystruct : public flex_struct {
int regular_member;
};
int main()
{
mystruct *s = new (100) mystruct; // mystruct with 100 chars extra
char *ptr = s->tail(); // get pointer to those 100 chars
memset(ptr, 0, 100); // fill them
delete s; // blow off struct and 100 chars
}