Hacker News new | ask | show | jobs
by dlsspy 5147 days ago
The two ways (C90 and C99) you can create a variable length item at the end of a structure in C are invalid C++. You can't express them at all.

I work around it by making a one element array and using "sizeof(MyThing) - 1" everywhere when I want to reference the size then use placement new and the ::new operator to do the allocation and initialization of my object.

It works, but it's not very straightforward.

In general, I rather like C++, but it does make a few things harder than C.

1 comments

You can't do it for structure fields in C++. The C++ idiom would probably be something like a member function that returned (void*)&this[1].