digitalmars.D.learn - get a struct member pointer
- Jeffry Nox (13/13) Feb 15 2009 struct A {
- Daniel Keep (15/31) Feb 15 2009 At runtime? You can't, really. The only thing you could do is
- Jeffry Nox (1/1) Feb 15 2009 ill try your suggestion, thx for reply.
- bearophile (5/9) Feb 16 2009 I am learning still this topic, but can't this create an aliasing proble...
- Daniel Keep (7/17) Feb 16 2009 Oh I really doubt the above is safe. But it's the only way I can think
- bearophile (4/6) Feb 16 2009 My browser there has done nothing bad, and that page offers one of the b...
- Daniel Keep (12/20) Feb 16 2009 Well, when it lists the site as having performed drive-by installation
- Chris Nicholson-Sauls (4/28) Feb 17 2009 Firefox/3.1 + NoScript + AdBlock-Plus prevents almost everything. That,...
- grauzone (5/15) Feb 16 2009 My theory is, that Walter's code generator is too primitive to care
- Daniel Keep (10/30) Feb 16 2009 Unless I cocked it up (which is entirely possible, mind you) I'm trying
- Christopher Wright (27/44) Feb 16 2009 Use tupleof. You need to specify a compile-time constant value as the
struct A { uint id=0; char[] name; } struct B { uint id=0; char[] title; } void lookup(T)(T[] s, ***) { char[] txt = s[0].***; } as illustrated above, how can i get struct object property info as in *** so i can manipulate it inside the function lookup above? in function lookup, i didnt know what is the variable name for the char[], so *** pass in the 2nd parameter, question is how to pass that member info or pointer so that it works on different struct declarations?
Feb 15 2009
Jeffry Nox wrote:struct A { uint id=0; char[] name; } struct B { uint id=0; char[] title; } void lookup(T)(T[] s, ***) { char[] txt = s[0].***; } as illustrated above, how can i get struct object property info as in *** so i can manipulate it inside the function lookup above? in function lookup, i didnt know what is the variable name for the char[], so *** pass in the 2nd parameter, question is how to pass that member info or pointer so that it works on different struct declarations?At runtime? You can't, really. The only thing you could do is something like this: void lookup(T)(T[] s, size_t offset) { char[] text = *cast(char[]*)(cast(void*)(&s[0]) + offset); } (Note: haven't tested that particular combination of casts; I don't bother memorising this :P) Or something to that effect. If possible, it'd be safer to do this: void lookup(T, char[] field)(T[] s) { char[] text = mixin(`s[0].`~field); } -- Daniel
Feb 15 2009
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
Feb 16 2009
bearophile wrote:Daniel Keep:Oh I really doubt the above is safe. But it's the only way I can think of to access a given struct field at runtime.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.htmlHeads up, Google is flagging the above site as doing drive-by installation of malicious software. Report here: http://safebrowsing.clients.google.com/safebrowsing/diagnostic?client=Firefox&hl=en-GB&site=http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.htmlBye, bearophile
Feb 16 2009
Daniel Keep:Heads up, Google is flagging the above site as doing drive-by installation of malicious software.My browser there has done nothing bad, and that page offers one of the best contents about this topic (I think that page is linked from Wikipedia too). Is that the kiss of death from Google for a page? :-) Bye, bearophile
Feb 16 2009
bearophile wrote:Daniel Keep:Well, when it lists the site as having performed drive-by installation WITHOUT having to prompt or notify the user, that's pretty much the end of it for me. Given the report itself, it's probably malware that got in via their advertiser. But that's still no excuse. I don't care HOW good the content is if I'm risking my machine to view it. That said, I actually read a bit of it via lynx; I don't think the code I listed violates strict aliasing. It creates a pointer of the same type to a piece of memory; it only creates one of a different type as an intermediary step, and doesn't store it. -- DanielHeads up, Google is flagging the above site as doing drive-by installation of malicious software.My browser there has done nothing bad, and that page offers one of the best contents about this topic (I think that page is linked from Wikipedia too). Is that the kiss of death from Google for a page? :-) Bye, bearophile
Feb 16 2009
Daniel Keep wrote:bearophile wrote:Firefox/3.1 + NoScript + AdBlock-Plus prevents almost everything. That, and running Linux in the first place... ;) -- Chris Nicholson-SaulsDaniel Keep:Well, when it lists the site as having performed drive-by installation WITHOUT having to prompt or notify the user, that's pretty much the end of it for me. Given the report itself, it's probably malware that got in via their advertiser. But that's still no excuse. I don't care HOW good the content is if I'm risking my machine to view it. That said, I actually read a bit of it via lynx; I don't think the code I listed violates strict aliasing. It creates a pointer of the same type to a piece of memory; it only creates one of a different type as an intermediary step, and doesn't store it. -- DanielHeads up, Google is flagging the above site as doing drive-by installation of malicious software.My browser there has done nothing bad, and that page offers one of the best contents about this topic (I think that page is linked from Wikipedia too). Is that the kiss of death from Google for a page? :-) Bye, bearophile
Feb 17 2009
bearophile wrote:Daniel Keep: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?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.htmlBye, bearophile
Feb 16 2009
grauzone wrote:bearophile wrote: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 -- DanielDaniel Keep: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?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.htmlBye, bearophile
Feb 16 2009
Jeffry Nox wrote:struct A { uint id=0; char[] name; } struct B { uint id=0; char[] title; } void lookup(T)(T[] s, ***) { char[] txt = s[0].***; } as illustrated above, how can i get struct object property info as in *** so i can manipulate it inside the function lookup above? in function lookup, i didnt know what is the variable name for the char[], so *** pass in the 2nd parameter, question is how to pass that member info or pointer so that it works on different struct declarations?Use tupleof. You need to specify a compile-time constant value as the index to tupleof, but looping also works: import tango.io.Stdout; struct S { int i; char[] b; } void getItem (S s, int index) { foreach (i, n; s.tupleof) { Stdout.formatln("{} {}", i, typeid(typeof(s.tupleof[i]))); if (i == index) { auto value = s.tupleof[i]; Stdout.formatln("Value is `{}`", value); } } } void main (char[][] args) { S s; getItem(s, 0); getItem(s, 1); }
Feb 16 2009