Stack Overflow

  While stacks are generally large, they don't occupy all of memory. It is possible to run out of stack space.

  For example, consider the code we had for factorial.  

1   int fact( int n ) {
2      if ( n == 0 )
3         return 1 ;
4      else
5         return fact( n - 1 ) * n ;
6    }

  Suppose fact(-1) is called. Then, the base case is never reached (well, it might be reached once we decrement so far that n wraps around to 0 again). This causes one stack frame after another to be pushed.

  Once the stack limit has been reached, we enter into invalid memory addresses, and the operating system takes over and kills your programming, telling you your program has a stack overflow.

  Probably the most common cause of stack overflow is a recursive function that doesn't hit the base case soon enough. For fans of recursion, this can be a problem, so just keep that in mind.

posted on 2015-05-08 16:04  mengdie  阅读(109)  评论(0编辑  收藏  举报