www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What's wrong with this code?

reply Sean Eskapp <eatingstaples gmail.com> writes:
I had some code that was segfaulting, so I rewrote the basic idea as a
fibonacci function, and lo and behold, it still segfaults. Why, and how to fix?
begin 644 main.d

M.PT*"0T*"71H:7,H*0T*"7L-" D);&5F="`](')I9VAT(#T ;G5L;#L-" E]

M=&AI<RYL969T(#T


M871E*"D *R!R:6=H="Y%=F%L=6%T92 I.PT*"7T-"GT-" T*1FEB($)A<BAI
M;G0 ;BD-"GL-" EI9BAN(#T



`
end
Jan 08 2011
next sibling parent reply Michal Minich <michal.minich gmail.com> writes:
On Sat, 08 Jan 2011 20:34:39 +0000, Sean Eskapp wrote:

 		if(left == null)
1) write if (left is null) instead if checking for null. Equality operator is rewritten to a.opEquals(b), which you don't want if you checking for null.
 this()
 {
     left = right = null;
 }
2) default constructor as you written it is not needed, fields are always initialized to default value of their type, which is in this case null.
 private const Fib* left, right;
3) Classes in D are reference types. It should be private const Fib left, right; 4) second constructor - Classes in D are reference types, & operator is not needed (see point 3), parameter left and right are already references to class.
Jan 08 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 08 Jan 2011 15:46:01 -0500, Michal Minich  
<michal.minich gmail.com> wrote:

 On Sat, 08 Jan 2011 20:34:39 +0000, Sean Eskapp wrote:

 		if(left == null)
1) write if (left is null) instead if checking for null. Equality operator is rewritten to a.opEquals(b), which you don't want if you checking for null.
Actually, this is no longer true. For comparing two classes or interfaces, a == b becomes .opEquals(a, b) which is defined in object.di and properly handles null references. However, if checking for null, a is null is going to be more correct and as efficient as possible (a == b should be inlined to the same thing, but only if you have inlining enabled). -Steve
Jan 10 2011
prev sibling next sibling parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Sean Eskapp napisa=B3(a):

 I had some code that was segfaulting, so I rewrote the basic idea as a=
 fibonacci function, and lo and behold, it still segfaults. Why, and ho=
w =
 to fix?
This looks fishy: class Fib { private const Fib* left, right; ... this(in Fib left, in Fib right) { this.left =3D &left; this.right =3D &right; } Are you sure you want a pointer to class? Classes have reference semanti= cs = in D (like Java). Structs are value types, though. Anyway, it looks like= = the ctor takes the address of the reference placed on the stack. -- = Tomek
Jan 08 2011
parent Sean Eskapp <eatingstaples gmail.com> writes:
Tomek got it right. Fixed by copying the objects, rather than using pointers.
Thanks!
Jan 08 2011
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Sean Eskapp:

 I had some code that was segfaulting, so I rewrote the basic idea as a
 fibonacci function, and lo and behold, it still segfaults. Why, and how to fix?
Two versions: struct Fib { private const Fib* left, right; this(in Fib* left=null, in Fib* right=null) { this.left = left; this.right = right; } const int evaluate() { if (left is null) return 1; else return left.evaluate() + right.evaluate(); } } Fib* bar(int n) { if (n == 0 || n == 1) return new Fib(); else return new Fib(bar(n - 1), bar(n - 2)); } void main() { auto x = bar(5); assert(x.evaluate() == 8); } -------------------- struct Fib { private const Fib* left, right; this(in Fib* left=null, in Fib* right=null) { this.left = left; this.right = right; } const int evaluate() { if (left is null) return 1; else return left.evaluate() + right.evaluate(); } } Fib* bar(int n) { if (n == 0 || n == 1) return new Fib(); else return new Fib(bar(n - 1), bar(n - 2)); } void main() { auto x = bar(5); assert(x.evaluate() == 8); } Bye, bearophile
Jan 08 2011
parent reply Michal Minich <michal.minich gmail.com> writes:
On Sat, 08 Jan 2011 16:39:34 -0500, bearophile wrote:

 Two versions:
Probably by mistake, both are the same.
Jan 08 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Michal Minich:

 Probably by mistake, both are the same.
You are right, I am sorry :-) In D the name of functions starts with lowercase. class Fib { private const Fib left, right; this(in Fib left=null, in Fib right=null) { this.left = left; this.right = right; } const int evaluate() { if (left is null) return 1; else return left.evaluate() + right.evaluate(); } } Fib bar(int n) { if (n == 0 || n == 1) return new Fib(); else return new Fib(bar(n - 1), bar(n - 2)); } void main() { auto x = bar(5); assert(x.evaluate() == 8); } Bye, bearophile
Jan 08 2011
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 09.01.2011 1:43, bearophile wrote:
 In D the name of functions starts with lowercase.
Or more precisely, in Phobos. There is no such requirement in D, I may suggest you stop using such a general and assertive posts, so not to confuse anyone. -- Dmitry Olshansky
Jan 11 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Dmitry Olshansky:

 Or more precisely, in Phobos.
 There is no such requirement in D,  I may suggest you stop using such a 
 general and assertive posts, so not to confuse anyone.
Let's create ecosystem-wide name requirements for D code, then! :-) The Phobos ones may be a good starting point. This topic is neglected, it needs a much wider discussion. Bye, bearophile
Jan 11 2011
parent spir <denis.spir gmail.com> writes:
On 01/11/2011 01:11 PM, bearophile wrote:
 Dmitry Olshansky:

 Or more precisely, in Phobos.
 There is no such requirement in D,  I may suggest you stop using such a
 general and assertive posts, so not to confuse anyone.
Let's create ecosystem-wide name requirements for D code, then! :-) The Phobos ones may be a good starting point. This topic is neglected, it needs a much wider discussion. Bye, bearophile
Anyway, it's wrong there is no style guide for D in general: http://www.digitalmars.com/d/2.0/dstyle.html The issue is: how can one expect programmers to follow guidelines the core itself language (esp builtin names) constantly breaks? Denis _________________ vita es estrany spir.wikidot.com
Jan 11 2011