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.