# Why stack overflow with such a small stack?

**URL:** <https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433>\
**Category:** Ruby\
**Created:** [August 31, 2007, 12:52am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433 "2007-08-31T00:52:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 12:52am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/1 "2007-08-31T00:52:05Z")

</div>

I’m getting the following error:

sheet.rb:35:in `values': stack level too deep (SystemStackError) from sheet.rb:35:in`values’  
from sheet.rb:53:in `to_s' from sheet.rb:52:in`to\_s’  
from sheet.rb:52:in `map' from sheet.rb:52:in`to\_s’  
from sheet.rb:52:in `to_s' from sheet.rb:52:in`map’  
from sheet.rb:52:in `to_s' from sheet.rb:52:in`to\_s’  
from sheet.rb:52:in `map' from sheet.rb:52:in`to\_s’  
from sheet.rb:92

The output above is given _in full_. Is Ruby truncating the call stack  
in this output? If so, how I can I see a larger snapshot of the stack,  
so I can trace down where the infinite recursion is really happening? If  
not, why am I getting this error?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 1:13am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/2 "2007-08-31T01:13:06Z")

</div>

Kenneth McDonald wrote:

> ```
> from sheet.rb:52:in `to_s'
> from sheet.rb:52:in `to_s'
> from sheet.rb:52:in `map'
> from sheet.rb:52:in `to_s'
> from sheet.rb:92
> 
> ```
> 
> The output above is given _in full_. Is Ruby truncating the call stack  
> in this output? If so, how I can I see a larger snapshot of the stack,  
> so I can trace down where the infinite recursion is really happening? If  
> not, why am I getting this error?

Here’s one even shorter:

> > def x; x; end; x  
> > SystemStackError: stack level too deep  
> > from (irb):2:in `x' from (irb):2:in`x’  
> > from (irb):4

Pretty smart of ruby to avoid a gazillion lines of identical output IMHO

Daniel

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 3:11am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/3 "2007-08-31T03:11:14Z")

</div>

Daniel DeLorme wrote:

> Kenneth McDonald wrote:
> 
> > I’m getting the following error:
> > 
> > sheet.rb:35:in `values': stack level too deep (SystemStackError) from sheet.rb:35:in`values’  
> > /\* SNIP \*/  
> > SystemStackError: stack level too deep  
> > from (irb):2:in `x' from (irb):2:in`x’  
> > from (irb):4
> 
> Pretty smart of ruby to avoid a gazillion lines of identical output IMHO
> 
> Daniel

It’s nice to see a smart little gem in them thar rubies 😉

I remmeber how long it took for a C Program to crash from the number of  
function calls… Dang thats a good Ruby.

TerryP.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 3:36am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/4 "2007-08-31T03:36:15Z")

</div>

Daniel DeLorme wrote:

> > ```
> > from sheet.rb:52:in `map'
> > 
> > ```

To a newcomer, it’s sorta nonobvious, however–it turned out the problem  
was that I’d made a circular reference to ‘values’ instead of ‘@values’,  
and the fact that the stack was full of ‘values’ frames is certainly not  
obvious from the above.

Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 3:56am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/5 "2007-08-31T03:56:19Z")

</div>

On Fri, 2007-31-08 at 10:35 +0900, Kenneth McDonald wrote:

> To a newcomer, it’s sorta nonobvious, however–it turned out the problem  
> was that I’d made a circular reference to ‘values’ instead of ‘@values’,  
> and the fact that the stack was full of ‘values’ frames is certainly not  
> obvious from the above.

A good rule of thumb for avoiding this problem is to name your classes  
and your variables after nouns and your methods after verbs.

So instead of having a method “values” that uses the member “@values” –  
something pretty much guaranteed to lead to trauma at some point down  
the road – make a method “get\_values” or “set\_values” or  
“initialize\_values” or whatever that uses a member called @values.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/6 "2007-08-31T16:26:58Z")

</div>

Heh, This is one of the few languages I haven’t had that happen to to  
me on before.

Lisp, C, C++, Perl… but I haven’t caused one of those on _ruby_  
before.

Now, if you’ve really intended to do recursion, try making it tail  
recursive.

Err… Ruby does optimize for tail recursion, right?

–Kyle

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 8:05am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/7 "2007-08-31T08:05:34Z")

</div>

On 31.08.2007 03:54, Michael T. Richter wrote:

> something pretty much guaranteed to lead to trauma at some point down  
> the road – make a method “get\_values” or “set\_values” or  
> “initialize\_values” or whatever that uses a member called @values.

Although I agree in general (i.e. using verbs for method names) I  
heavily disagree with your last advice: according to Ruby’s convention a  
getter is named “foo” and a setter “foo=”. This is implemented in attr\*  
methods and IMHO it is not advisable to leave this convention.

Also, there are good reasons to use getters instead of instance  
variables, namely that you get more abstraction (there doesn’t  
necessarily have to be a member variable of that name).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/8 "2007-08-31T18:33:13Z")

</div>

On 8/31/07, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Heh, This is one of the few languages I haven’t had that happen to to  
> me on before.
> 
> Lisp, C, C++, Perl… but I haven’t caused one of those on _ruby_ before.
> 
> Now, if you’ve really intended to do recursion, try making it tail recursive.
> 
> Err… Ruby does optimize for tail recursion, right?

I’m not sure, after some googling it seems that 1.8 doesn’t and YARV  
might partially optimize.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 1, 2007, 6:22am UTC](https://www.ruby-forum.com/t/why-stack-overflow-with-such-a-small-stack/113433/9 "2007-09-01T06:22:10Z")

</div>

Unless I read otherwise, I usually assume that languages don’t optimize  
tail recursion. If you look at typical codebases, using tail recursion  
instead of some sort of higher-level looping construct, or even a simple  
basic loop, is really an academic curiosity. It doesn’t normally make  
code clearer or easier to write, and most people, even those who are  
aware of it, don’t use it. (In fact people who really know about it  
often tend to avoid it because they also know of the overhead induced by  
function calling.) So why bother optimizing for this case?

Ken
