www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct template help

reply "seany" <seany uni-bonn.de> writes:
Please consider the following

struct arc(T,U)
{

T some_var;
U  someother_var;
}

/* things */

class myclass
{
   this(){}
  ~this(){}
   void MYfunction()

   {
     arc!(string, string[]) * a;
     a.some_var = "hello";
   }
}


void main()
{
   c = new myclass();
   c.MYfunction();
}


This leads to a segmentation fault.

What am I doing wrong?
Jul 12 2014
next sibling parent reply "seany" <seany uni-bonn.de> writes:
Also, (*c).MYfunction() is leading to segmentation fault
Jul 12 2014
parent "seany" <seany uni-bonn.de> writes:
sorry, I meant (*a).some_var
not (*c).MYfunction()
Jul 12 2014
prev sibling parent reply "Danyal Zia" <catofdanyal yahoo.com> writes:
On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote:
 Please consider the following

 struct arc(T,U)
 {

 T some_var;
 U  someother_var;
 }

 /* things */

 class myclass
 {
   this(){}
  ~this(){}
   void MYfunction()

   {
     arc!(string, string[]) * a;
     a.some_var = "hello";
   }
 }


 void main()
 {
   c = new myclass();
   c.MYfunction();
 }


 This leads to a segmentation fault.

 What am I doing wrong?
"a" has not been instantiated. You are declaring it as a pointer to struct and using its fields without initializing it. "arc!(string, string[]) a;" will work.
Jul 12 2014
parent reply "seany" <seany uni-bonn.de> writes:
On Saturday, 12 July 2014 at 19:16:52 UTC, Danyal Zia wrote:
 On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote:
 Please consider the following

 struct arc(T,U)
 {

 T some_var;
 U  someother_var;
 }

 /* things */

 class myclass
 {
  this(){}
 ~this(){}
  void MYfunction()

  {
    arc!(string, string[]) * a;
    a.some_var = "hello";
  }
 }


 void main()
 {
  c = new myclass();
  c.MYfunction();
 }


 This leads to a segmentation fault.

 What am I doing wrong?
"a" has not been instantiated. You are declaring it as a pointer to struct and using its fields without initializing it. "arc!(string, string[]) a;" will work.
For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please?
Jul 12 2014
next sibling parent reply "Danyal Zia" <catofdanyal yahoo.com> writes:
On Saturday, 12 July 2014 at 19:19:28 UTC, seany wrote:
 For reasons further down in the software, I need to do this 
 with a pointer. How do I do it with a pointer, please?
I don't know what are you trying to achieve, but if that's what you want, you can do: void MYfunction() { auto strArr = ["Hello", "World!"]; arc!(string, string[]) * a = new arc!(string, string[])("s", strArr); a.some_var = "hello"; } If you want to use "a" from outside the function you have to declare it in the class and then initialize it in your function.
Jul 12 2014
parent reply "seany" <seany uni-bonn.de> writes:
do I have to initialize all variables of the struct? or may I 
also use a this(){} in the struct and initialize only those which 
are known at a given moment?
Jul 12 2014
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/12/2014 12:32 PM, seany wrote:

 do I have to initialize all variables of the struct?
No. The uninitialized ones get their .init values.
 or may I also use a
 this(){} in the struct and initialize only those which are known at a
 given moment?
That already works with structs. You don't need to define any constructor if the type is simple data i.e. if you don't want to set any invariants, just construct with whatever is available at that moment: auto a = new S("hello"); However, if it is important that someother_var should not remain as an empty array, then you better define a constructor and demand both values when constructing. Ali
Jul 12 2014
prev sibling parent reply "Danyal Zia" <catofdanyal yahoo.com> writes:
On Saturday, 12 July 2014 at 19:32:48 UTC, seany wrote:
 do I have to initialize all variables of the struct? or may I 
 also use a this(){} in the struct and initialize only those 
 which are known at a given moment?
You can initialize in constructor this(), but you can't initialize partial fields of struct when using pointer to struct. There would be other trivial ways to do what you are trying to do. Also, I never used any pointer to struct (which is of value type) in my code .
Jul 12 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/12/2014 12:38 PM, Danyal Zia wrote:

 You can initialize in constructor this(), but you can't initialize
 partial fields of struct when using pointer to struct.
Actually, that works too but members must be initialized from the beginning. The trailing ones are left with .init values: struct S { int i; string s; } void main() { auto s = new S(42); static assert(is (typeof(s) == S*)); } Ali
Jul 12 2014
parent "Danyal Zia" <catofdanyal yahoo.com> writes:
On Saturday, 12 July 2014 at 19:42:13 UTC, Ali Çehreli wrote:
 Actually, that works too but members must be initialized from 
 the beginning. The trailing ones are left with .init values:

 struct S
 {
     int i;
     string s;
 }

 void main()
 {
     auto s = new S(42);
     static assert(is (typeof(s) == S*));
 }

 Ali
Ah, right. I still has C++ background in my veins I guess :)
Jul 12 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/12/2014 12:19 PM, seany wrote:

 On Saturday, 12 July 2014 at 19:16:52 UTC, Danyal Zia wrote:
 On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote:
    arc!(string, string[]) * a;
    a.some_var = "hello";
 "a" has not been instantiated. You are declaring it as a pointer to
 struct and using its fields without initializing it. "arc!(string,
 string[]) a;" will work.
For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please?
By making that pointer point to an actual object. :) arc!(string, string[]) * a = new arc!(string, string[])(); Shorter syntax: auto a = new arc!(string, string[]); You can assign it later as well: arc!(string, string[]) * a; // ... a = new arc!(string, string[])(); Preferably, don't assign to the member but construct when the information is available: auto a = new arc!(string, string[])("hello", ["merhaba", "hola", "bonjour"]); Ali
Jul 12 2014