zoukankan      html  css  js  c++  java
  • Embedded之Stack之三

    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.

  • 相关阅读:
    第四次作业
    第三次作业
    第二次作业。
    国庆作业。
    实验2-4
    实验2-3
    实验2-2
    实验2-1
    实验1-3
    实验 1-1
  • 原文地址:https://www.cnblogs.com/mengdie/p/4487963.html
Copyright © 2011-2022 走看看