Strange seg fault
Ali Çehreli
acehreli at yahoo.com
Wed Jun 12 13:49:54 PDT 2013
On 06/12/2013 01:12 PM, gedaiu wrote:
> Hi,
>
> Can anyone help me why this code goes wrong? I get exit code 139 with
> DMD 2.063
>
>
> import std.stdio;
>
>
>
> struct Test {
> string val = "";
> string[] key;
>
> /**
> * operator "=" overload
> */
> Test opAssign(string val){
> this.val = val;
>
> string[1] t;
> t = val;
>
> key ~= t;
Instead of the last three lines:
key ~= val;
However, if you really wanted a 1-element fixed-length array then:
string[1] t = [ val ];
> return this;
> }
>
Remove the following opAssign altogether:
> Test opAssign(Test val){
> this.val = val.val;
>
> key ~= val.key;
>
> return this;
> }
As general rules:
* Define a post-blit only if the code would be incorrect if you don't do
that. (For example, it may be incorrect that two objects share the same
array.)
* Define an opAssign from the same type only if your implementation will
be more efficient than the compiler's safe alternative, which happens to
be "first copy, then swap." For example, instead of copying a member
array, you may decide to reuse it.
> void append(Test t) {
> val ~= t.val;
> key ~= t.key;
> };
> }
>
> void main() {
>
> Test val;
> val = "test";
>
> Test[string] data;
> Test v;
> v = "asd";
> data["test"] = v;
>
> writeln(v);
> writeln(data["test"]);
>
> data["test"].append(val);
>
>
> writeln(data["test"].key);
> writeln("done");
> }
>
> Thanks,
> Bogdan
Ali
More information about the Digitalmars-d-learn
mailing list