www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointer to Struct Pointer

reply Oliver <oliver.ruebenkoenig web.de> writes:
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
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent Oliver <oliver.ruebenkoenig web.de> writes:
== Quote from bearophile (bearophileHUGS lycos.com)'s article
 Oliver:
 ...

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
Feb 19 2011
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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;
      }
 }

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
parent oliver <oliver.ruebenkoenig web.de> writes:
== 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.


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.


 runs successfully.  At least in 2.051 Windows.  Is something different
happening

 setup?

No, no that was a mixup on my side.
 I do not understand how I dereferece the ts struct correctly.


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;
      }
 }

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.

Feb 19 2011