get a struct member pointer

Daniel Keep daniel.keep.lists at gmail.com
Mon Feb 16 05:42:25 PST 2009



grauzone wrote:
> bearophile wrote:
>> Daniel Keep:
>>> void lookup(T)(T[] s, size_t offset)
>>> {
>>>   char[] text = *cast(char[]*)(cast(void*)(&s[0]) + offset);
>>> }
>>
>> I am learning still this topic, but can't this create an aliasing
>> problem, as in C?
>> http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html
>>
> 
> My theory is, that Walter's code generator is too primitive to care
> about aliasing. But I guess it's possible, that aliasing rules will be
> added later to the language, when LDC (hopefully) gets big?
> 
> By the way, wtf is Daniel's code doing at all?
> 
>> Bye,
>> bearophile

Unless I cocked it up (which is entirely possible, mind you) I'm trying
to get a pointer to the struct, cast to void* because I can never
remember if (ptr + int) multiplies the offset by (*ptr).sizeof or not,
casting THAT to a pointer to a char[], then dereferencing it to get the
value.

It is also, in a round-about way, trying to demonstrate that trying to
do this is really just not pretty and Jeffry might like to investigate
alternate ways of getting those fields.  :P

  -- Daniel


More information about the Digitalmars-d-learn mailing list