Exception caused by calling a pure function repeatedly

Ali Çehreli acehreli at yahoo.com
Tue May 29 23:06:09 PDT 2012


On 05/29/2012 07:33 PM, ixid wrote:
> Having solved Project Euler 300 I wanted to explore which of the
> strictly superior fold set were redundant but started getting an
> exception thrown after a few iterations.
>
> I've changed the offending part to try to make it as impact-free as
> possible (my code is not pretty :)), it's now pure and the return is
> only used with writeln to track the progress of the function yet it
> still causes an exception after 4 or 5 iterations.
>
> http://pastebin.com/j6REskhd
>
> The offending function is 'solve', which I've repeatedly called to make
> the exception as simple as possible along with const data and limited
> use of the return data. I've tried making it pure/const as well as
> adding delete on the main data and also removing pure and calling the
> garbage collector. This is the stack trace crash assembler command:
>
> 7C90E8E5 push ebx
>
> Does this mean there is a stack overflow? Any ideas what on earth is
> going on?

I haven't even run your program but I think your guess is correct: You 
are probably running out of stack space. The reason must be the fact 
that you pass fixed-length arrays by value.

LEN being 15, especially 'bits' is a very large array:

pure int[] solve(const bool[] redundancy, const ushort[LEN - 1][] 
matrixes, const int[2^^LEN] bits, const int[] numbers)

Fixed-length arrays are value types and are copied on the stack. Try 
passing 'bits' as 'const ref' instead of just 'const'.

Ali

-- 
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html


More information about the Digitalmars-d-learn mailing list