Like Tree2Likes

EStackOverFlow

This is a discussion on EStackOverFlow within the C++ Programming forums, part of the General Programming Boards category; When run the code below: Code: void F() { F(); } EStackOverFlow will appear. The book says that there is ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    19

    Post EStackOverFlow

    When run the code below:
    Code:
    void F()
    {
    
      F();
    
    }
    EStackOverFlow will appear.
    The book says that there is a loop going on. Can anyone explain to me?
    Because I think in the Void F function, it will only use itself once.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    You're looking at infinite recursion. Yes, F only calls itself once, but the call to F calls itself once, which calls itself once, which... the sum of all energy-matter in the universe is insufficient for me to express this in full.
    Fordy and rags_to_riches like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    Oh. I get it.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    What about the following code?
    Code:
    void F(int a)
    
             {
           F(a+1);
             }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    What's the terminating condition (or base case)?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    Umm.. What is the meaning of 'terminating condition' ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    Didn't your book give you examples of recursion?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    Sorry. my book is in Chinese.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    Okay, basically: when does the recursion end? Trace through the code and find out.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    When the condition is not satisfied.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    So, what is the condition?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    In the code I have given ? Or?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    Yes, the code in post #4.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    19
    um.. after f(a+1)?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    Is that an answer to my question or is that a question?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21