Re: get a struct member pointer

2009-02-16 Thread bearophile
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

Bye,
bearophile


Re: get a struct member pointer

2009-02-16 Thread grauzone

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


Re: get a struct member pointer

2009-02-16 Thread Daniel Keep


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


Re: get a struct member pointer

2009-02-15 Thread Jeffry Nox
ill try your suggestion, thx for reply.