Öö Tiib wrote:
> On 28 aug, 19:15, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
>> Juha Nieminen <nos...@thanks.invalid> wrote
>> innews:4c792d06$0$32119$:
>>
>>
>>
>>> joe <jc1...@att.net> wrote:
>>>> 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.
> If it is
> created into storage with compile-time constant size (256 bytes)
> then ... what was the point again?
|