digitalmars.D.learn - Pointer to Struct Pointer
- Oliver (37/37) Feb 19 2011 Hello,
- bearophile (35/36) Feb 19 2011 struct Ts {
- Oliver (4/39) Feb 19 2011 Thanks for the comments.
- Stewart Gordon (13/30) Feb 19 2011 The code you've posted throws an AV, and correctly so. If you change ts...
- oliver (8/41) Feb 19 2011 a ts, it
Hello, I have the following code that works. I'd like to change the t tsData to ts tsData, but this does segfault. I do not understand how I dereferece the ts struct correctly. Perhaps someone could give me a hint. Thanks. Oliver ----------------- alias rs *r; alias ts *t; struct ts { int d; union { int[] intData; double[] doubleData; } } struct rs { int i; union { int intData; //ts tsData; t tsData; }; this( int i, int d, ref int[] data) { this.i = i; this.tsData.d = d; this.tsData.intData = data; } } r makeData(int i, int d, int[] data) { return new rs(i,d,data); } int main () { int i = 1; int[] data = [1,2]; auto test = makeData(i,1,data); return 0; }
Feb 19 2011
Oliver:...struct Ts { int d; union { int[] intData; double[] doubleData; } } struct Rs { int i; union { int intData; Ts* tsData; } this(int i, int d, int[] data) { this.i = i; this.tsData = new typeof(*tsData); this.tsData.d = d; this.tsData.intData = data; } } Rs* makeData(int i, int d, int[] data) { return new Rs(i, d, data); } void main () { int i = 1; int[] data = [1, 2]; auto test = makeData(i, 1, data); } - You have not created the pointed inner struct. - Don't use aliases like those ones, they confuse the code. - Struct names are better with a starting uppercase letter in D. Bye, bearophile
Feb 19 2011
== Quote from bearophile (bearophileHUGS lycos.com)'s articleOliver:Yes, thanks, I got it....struct Ts { int d; union { int[] intData; double[] doubleData; } } struct Rs { int i; union { int intData; Ts* tsData; } this(int i, int d, int[] data) { this.i = i; this.tsData = new typeof(*tsData); this.tsData.d = d; this.tsData.intData = data; } } Rs* makeData(int i, int d, int[] data) { return new Rs(i, d, data); } void main () { int i = 1; int[] data = [1, 2]; auto test = makeData(i, 1, data); } - You have not created the pointed inner struct.- Don't use aliases like those ones, they confuse the code. - Struct names are better with a starting uppercase letter in D. Bye, bearophileThanks for the comments. Olier
Feb 19 2011
On 19/02/2011 13:18, Oliver wrote:Hello, I have the following code that works.What? The code you've posted doesn't work.I'd like to change the t tsData to ts tsData, but this does segfault.The code you've posted throws an AV, and correctly so. If you change tsData to a ts, it runs successfully. At least in 2.051 Windows. Is something different happening on your setup?I do not understand how I dereferece the ts struct correctly.You don't dereference a struct, you dereference a pointer. <snip>struct rs { int i; union { int intData; //ts tsData; t tsData; }; this( int i, int d, ref int[] data) { this.i = i; this.tsData.d = d; this.tsData.intData = data; } }<snip> You have not initialised tsData. Consequently, you are trying to dereference a null pointer. Hence the segfault. For it to work with tsData being a pointer, you need to add something like this: this.tsData = new ts; Stewart.
Feb 19 2011
== Quote from Stewart Gordon (smjg_1998 yahoo.com)'s articleOn 19/02/2011 13:18, Oliver wrote:Sorry for that, in the post I got the // and the second one mixed up.Hello, I have the following code that works.What? The code you've posted doesn't work.a ts, itI'd like to change the t tsData to ts tsData, but this does segfault.The code you've posted throws an AV, and correctly so. If you change tsData toruns successfully. At least in 2.051 Windows. Is something different happeningon yoursetup?No, no that was a mixup on my side.Yea, well that sould have read dereference the pointer to the struct.I do not understand how I dereferece the ts struct correctly.You don't dereference a struct, you dereference a pointer.<snip>Thanks, that was what I did not get - for what ever reason - which seems obvious now. Oliverstruct rs { int i; union { int intData; //ts tsData; t tsData; }; this( int i, int d, ref int[] data) { this.i = i; this.tsData.d = d; this.tsData.intData = data; } }<snip> You have not initialised tsData. Consequently, you are trying to dereference a null pointer. Hence the segfault. For it to work with tsData being a pointer, you need to add something like this: this.tsData = new ts;Stewart.
Feb 19 2011