Zero-size array as struct member

J

joe

Öö Tiib said:
in

Alf P. Steinbach /Usenet wrote:
Please post code that shows the raw struct hack in action without
using a pointer.
int main()
{
unsigned char buff[256];
msg* m =
new((void*)buff) msg(MSG_ID_HELLO, 244, 13, "Hello world!");
sendmsg(m);
return 0;
}
What do you think that 'm' is if not a pointer?
And your code perfectly exemplifies why that pointer should be
abstracted
away in C++. You are leaking it.

Sorry, but there is no leak (cannot be as there is no dynamic memory
allocation). And one can easily get rid of the m pointer, it is not
needed here, one could just sendmsg(buff).

OTOH, this code exhibits UB because buff is not guaranteed to be
aligned
properly for msg. But this can be fixed.

It can be fixed yes. Like they say here that the whole point of struct
hack is to have something that has run-time decided size.

Well I didn't say that, maybe someone else did. The point is to get
something contiguous that can vary in length, not necessarily just at
runtime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top