Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Zero-size array as struct member

Reply
Thread Tools

Zero-size array as struct member

 
 
joe
Guest
Posts: n/a
 
      08-28-2010
Öö 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?



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Using an instance of a struct as a member of that struct dutchgoldtony C Programming 15 11-16-2005 11:24 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57