OK, the lightbulb went on.  Thanks guys.

Cheers,
Josh


On Jun 8, 2011, at 12:35 PM, BGB wrote:

> On 6/8/2011 12:13 PM, Josh Gargus wrote:
>> 
>> 
>> On Jun 8, 2011, at 11:56 AM, Kevin Jones wrote:
>> 
>>> Comments and explanations inline below.
>>> 
>>> --- On Wed, 6/8/11, Julian Leviston <[email protected]> wrote:
>>> 
>>> Also, what is this?
>>> 
>>> struct vtable *_vt[0];
>>> 
>>> Is that creating a pointer to a struct array which has zero elements in it? 
>>> I don't follow what that's for?
>>> 
>>> This is an idiom in C for variable-length arrays when at the end of a 
>>> struct definition.
>>> 
>> 
>> Thanks Kevin,
>> 
>> Can you elaborate a bit more on this idiom?  Is it purely 
>> intention-revealing code, to signify that it's a pointer to an array of 
>> structs instead of a single struct?  In other words, does it compile to the 
>> same thing as "struct vtable *_vt;" ?  
>> 
> 
> nope...
> 
> it only states where the start of the array is, in the struct, but no actual 
> storage is reserved in the struct (any array contents are assumed to exist 
> following the end of the struct).
> 
> typedef struct Foo_s Foo;
> struct Foo_s {
> int x;
> float y[0];
> };
> 
> Foo *p;
> p=malloc(sizeof(Foo)+10*sizeof(float));    //allocate struct, + the size of 
> 10 floats.
> 
> p->y[5];    //accesses the float at element 5, which is past the end of the 
> struct
> p->y[-1];    //happens to be at the same address as p->x on typical systems
> ...
> 
> to really understand this though would require to understand pointers, and 
> how C lays things out in memory.
> 
> say, p==0x006F4000
> 
> and addresses:
> 0x006F4000    p->x
> 0x006F4004    p->y[0]
> 0x006F4008    p->y[1]
> 0x006F400C    p->y[2]
> ...
> 
> or, laid out as bytes:
> <XX XX XX XX> <Y0 Y0 Y0 Y0> <Y1 Y1 Y1 Y1> <Y2 Y2 Y2 Y2>
> 
> or, if p->x=0x12345678, p->y[0]=1.0; p->y[1]=0; and using little-endian 
> ordering:
> <78 56 34 12> <00 00 80 3F> <00 00 00 00> ...
> 
> 
> dunno if this helps...
> 
> 

_______________________________________________
fonc mailing list
[email protected]
http://vpri.org/mailman/listinfo/fonc

Reply via email to