Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 19, 2011 Pointer to Struct Pointer | ||||
---|---|---|---|---|
| ||||
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; } |
February 19, 2011 Re: Pointer to Struct Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oliver | 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
|
February 19, 2011 Re: Pointer to Struct Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oliver | 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. |
February 19, 2011 Re: Pointer to Struct Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | == Quote from bearophile (bearophileHUGS@lycos.com)'s article > 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. Yes, thanks, I got it. > - Don't use aliases like those ones, they confuse the code. > - Struct names are better with a starting uppercase letter in D. > Bye, > bearophile Thanks for the comments. Olier |
February 19, 2011 Re: Pointer to Struct Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | == Quote from Stewart Gordon (smjg_1998@yahoo.com)'s article > 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. Sorry for that, in the post I got the // and the second one mixed up. > > 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? No, no that was a mixup on my side. > > I do not understand how I dereferece the ts struct correctly. > You don't dereference a struct, you dereference a pointer. Yea, well that sould have read dereference the pointer to the struct. > <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; Thanks, that was what I did not get - for what ever reason - which seems obvious now. Oliver > Stewart. |
Copyright © 1999-2021 by the D Language Foundation