Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GC Error when use BigInt #2401

Closed
ethe opened this issue Apr 3, 2016 · 2 comments
Closed

GC Error when use BigInt #2401

ethe opened this issue Apr 3, 2016 · 2 comments

Comments

@ethe
Copy link

ethe commented Apr 3, 2016

Simple fibonacci function like this:

require "big_int"

def fib(n : Int32, ret1 : BigInt = BigInt.new("0"), ret2 : BigInt = BigInt.new("1"))
  if n == 0
    return ret1
  else
    return fib(n-1, ret2, ret1+ret2)
  end
end

puts fib(100000)

Got below Error:

Invalid memory access (signal 11) at address 0x7ffeaabdccf8
[4343989] *CallStack::print_backtrace:Int32 +117
[4334072] __crystal_sigfault_handler +56
[4447960] sigfault_handler +40
[139850285739280] ???
[4556322] GC_clear_stack_inner +11
[4556406] GC_clear_stack_inner +95 (9 times)
[4556559] GC_clear_stack +129
[4533012] GC_core_malloc +358
[4578165] GC_malloc +148
[4534821] GC_generic_or_special_malloc +72
[4535354] GC_realloc +484
[4328152] __crystal_realloc +8
[4346090] *GC::realloc<Pointer(Void), UInt64>:Pointer(Void) +10
[4335821] ~proc(Pointer(Void), UInt64, UInt64 -> Pointer(Void)) +13
[139850290158250] __gmpz_realloc +58
[139850290092705] __gmpz_add +913
[4447012] *BigInt#+<BigInt, BigInt>:BigInt +116
[4336233] *fib<Int32, BigInt, BigInt>:BigInt +105
[4336295] *fib<Int32, BigInt, BigInt>:BigInt +167 (87117 times)
[4336091] *fib<Int32>:BigInt +251
[4328066] ???
[4335609] main +41
[139850277526080] __libc_start_main +240
[4325193] _start +41
[0] ???

Use Crystal 0.15.0, LLVM 3.6.2.

BTW, do you think syntax like def fib(n : Int32, ret1 : BigInt = BigInt.new("0"), ret2 : BigInt = BigInt.new("1")) seems too looooong?

@ethe ethe changed the title GC Error when use BitInt GC Error when use BigInt Apr 3, 2016
@asterite
Copy link
Member

asterite commented Apr 3, 2016

I think it's just a stack overflow. I also tried this in Ruby and get a stack overflow too:

def fib(n, ret1 = 0, ret2 = 1)
  if n == 0
    return ret1
  else
    return fib(n - 1, ret2, ret1 + ret2)
  end
end

puts fib(100000)

It has nothing to do with the GC because if you run it with GC_DONT_GC=1 ... you still get a crash. The stack mentions the GC because the GC call probably needs more stack levels and that's where the error happens.

I say we should close this and relate it to #271 (stack overflows should say "stack overflow" when such error happens).

@ethe
Copy link
Author

ethe commented Apr 3, 2016

@asterite Ok, but Ruby does not support Tail Call Optimization, so I'm not surprised get stack overflow, seems Crystal support it, fib(10000) is no problem.

@ethe ethe closed this as completed Apr 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants