Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. stack overflow

stack overflow

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestioncsharpvisual-studio
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lord_Vader
    wrote on last edited by
    #1

    Hello, I have two questions about the stack. 1st)In MS visual studio the stack is 1MB as far as I now. I found that using a recursive function the stack overflows at depth ~4900. Why does this happen? 2nd)If someone has a linked list with thousand of elements how he can reach the whole list using a recursive function? thanks.

    L N 2 Replies Last reply
    0
    • L Lord_Vader

      Hello, I have two questions about the stack. 1st)In MS visual studio the stack is 1MB as far as I now. I found that using a recursive function the stack overflows at depth ~4900. Why does this happen? 2nd)If someone has a linked list with thousand of elements how he can reach the whole list using a recursive function? thanks.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Lord_Vader wrote:

      1st)In MS visual studio the stack is 1MB as far as I now.

      Lord_Vader wrote:

      Why does this happen?

      When you reach the limit, regardless of what that is, you get the overflow error. What is so hard to understand about that?

      Lord_Vader wrote:

      2nd)If someone has a linked list with thousand of elements how he can reach the whole list using a recursive function?

      Why would you want to do that?

      L 1 Reply Last reply
      0
      • L led mike

        Lord_Vader wrote:

        1st)In MS visual studio the stack is 1MB as far as I now.

        Lord_Vader wrote:

        Why does this happen?

        When you reach the limit, regardless of what that is, you get the overflow error. What is so hard to understand about that?

        Lord_Vader wrote:

        2nd)If someone has a linked list with thousand of elements how he can reach the whole list using a recursive function?

        Why would you want to do that?

        L Offline
        L Offline
        Lord_Vader
        wrote on last edited by
        #3

        1st)Its not hard to understand that. But I want someone to explain specifically what happens, for example how much stack memory one function call consumes why... 2nd)Because I have a linked list with thousand elements and I wondering if it is possible to go through it using recursive functions.

        C J 2 Replies Last reply
        0
        • L Lord_Vader

          1st)Its not hard to understand that. But I want someone to explain specifically what happens, for example how much stack memory one function call consumes why... 2nd)Because I have a linked list with thousand elements and I wondering if it is possible to go through it using recursive functions.

          C Offline
          C Offline
          Cyrilix
          wrote on last edited by
          #4

          Why would you iterate through a linked list with a recursive function? You already figured out the answer to your question: it is not possible unless you increase the stack size, since you seem to top at around 4900. Just call the next pointer until you reach the end of the list. It only takes a single function.

          L 1 Reply Last reply
          0
          • L Lord_Vader

            Hello, I have two questions about the stack. 1st)In MS visual studio the stack is 1MB as far as I now. I found that using a recursive function the stack overflows at depth ~4900. Why does this happen? 2nd)If someone has a linked list with thousand of elements how he can reach the whole list using a recursive function? thanks.

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            In general, using recursion in pure imperative languages such as C++ is a bad idea - it can lead to stack overflows and also tends to be slower than iteration. Functional languages such as OCaml are able to internaly convert recursion (at least tail recursion) to iteration and then it is OK to use recursion.


            Programming Blog utf8-cpp

            C 1 Reply Last reply
            0
            • C Cyrilix

              Why would you iterate through a linked list with a recursive function? You already figured out the answer to your question: it is not possible unless you increase the stack size, since you seem to top at around 4900. Just call the next pointer until you reach the end of the list. It only takes a single function.

              L Offline
              L Offline
              Lord_Vader
              wrote on last edited by
              #6

              Ok thanks,I was going to do that, but I posted mainly to learn the details behind a functions call..

              1 Reply Last reply
              0
              • L Lord_Vader

                1st)Its not hard to understand that. But I want someone to explain specifically what happens, for example how much stack memory one function call consumes why... 2nd)Because I have a linked list with thousand elements and I wondering if it is possible to go through it using recursive functions.

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                Stack memory is generally required for the function call information (return values and return address), as well as for the automatic variables in the function being called.  One function may consume only 32 bytes of stack while another consumes more than 8KB of stack.    It is possible to recursively iterate over any size list...   Given you have enough stack space and your environment supports it.  Realistically, this depends on (1) how much stack is consumed by the recursive function, (2) how many elements are in the list, and (3) how much free stack space you have remaining at the time you start recursion (just because you have 1MB of stack by default in Win32, that does not mean that by the time your function is first called that you will have much of it left).    Iteration is generally better done using loops instead of recursion.  It is also easier to debug, because you can more easily break at a certain loop counter than a certain recursion depth.    Peace!

                -=- James
                Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                See DeleteFXPFiles

                L realJSOPR 2 Replies Last reply
                0
                • N Nemanja Trifunovic

                  In general, using recursion in pure imperative languages such as C++ is a bad idea - it can lead to stack overflows and also tends to be slower than iteration. Functional languages such as OCaml are able to internaly convert recursion (at least tail recursion) to iteration and then it is OK to use recursion.


                  Programming Blog utf8-cpp

                  C Offline
                  C Offline
                  Cyrilix
                  wrote on last edited by
                  #8

                  Well, I think there are definitely some very good uses of recursion in C++ such as generically printing out the data in an XML DOM tree, where there is no absolutely defined node structure. I'm not sure how you'd go about doing that otherwise. That said, DOM tree hierarchies typically don't go down that many levels -- that would be both a memory usage and usability nightmare.

                  1 Reply Last reply
                  0
                  • J James R Twine

                    Stack memory is generally required for the function call information (return values and return address), as well as for the automatic variables in the function being called.  One function may consume only 32 bytes of stack while another consumes more than 8KB of stack.    It is possible to recursively iterate over any size list...   Given you have enough stack space and your environment supports it.  Realistically, this depends on (1) how much stack is consumed by the recursive function, (2) how many elements are in the list, and (3) how much free stack space you have remaining at the time you start recursion (just because you have 1MB of stack by default in Win32, that does not mean that by the time your function is first called that you will have much of it left).    Iteration is generally better done using loops instead of recursion.  It is also easier to debug, because you can more easily break at a certain loop counter than a certain recursion depth.    Peace!

                    -=- James
                    Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    See DeleteFXPFiles

                    L Offline
                    L Offline
                    Lord_Vader
                    wrote on last edited by
                    #9

                    Thanks, Here is another one. I additionally have a bin tree with many thousands of elements. I use recursive functions to walk through the leafs because its more intuitive. Will it be faster if I use loops instead?

                    J 1 Reply Last reply
                    0
                    • L Lord_Vader

                      Thanks, Here is another one. I additionally have a bin tree with many thousands of elements. I use recursive functions to walk through the leafs because its more intuitive. Will it be faster if I use loops instead?

                      J Offline
                      J Offline
                      James R Twine
                      wrote on last edited by
                      #10

                      It may be faster to use loops, depending on the setup required for the recursive function call (stack frame setup, construction/destruction of any local objects, exception handling setup if used, etc.).  If your function uses poor coding habits, like using a CString instead of a local buffer for simple formatting, using the loop will very likely be faster than recursion, because you can eliminate the overhead of allocating/deallocating the memory for the string.  (You may not notice a difference when only calling the function 100 or so times, but when you start hitting the 1000+ ranges, you may.)    A (well-balanced) binary tree is likely to have less recursive demand, whereas a list is a linear structure so your recursion level would be equal to the number of items in the list.  Trees tend to have less depth when used/populated correctly.    Peace!

                      -=- James
                      Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                      See DeleteFXPFiles

                      L 1 Reply Last reply
                      0
                      • J James R Twine

                        Stack memory is generally required for the function call information (return values and return address), as well as for the automatic variables in the function being called.  One function may consume only 32 bytes of stack while another consumes more than 8KB of stack.    It is possible to recursively iterate over any size list...   Given you have enough stack space and your environment supports it.  Realistically, this depends on (1) how much stack is consumed by the recursive function, (2) how many elements are in the list, and (3) how much free stack space you have remaining at the time you start recursion (just because you have 1MB of stack by default in Win32, that does not mean that by the time your function is first called that you will have much of it left).    Iteration is generally better done using loops instead of recursion.  It is also easier to debug, because you can more easily break at a certain loop counter than a certain recursion depth.    Peace!

                        -=- James
                        Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                        See DeleteFXPFiles

                        realJSOPR Offline
                        realJSOPR Offline
                        realJSOP
                        wrote on last edited by
                        #11

                        Each function call in a recursive loop consumes stack space. If you're passing parameters, that consumes even more stack space. I thought the max stack space for a given app was limited to 64k... Of course, I'm old, so it may have changed since I last checked it out. :)

                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                        1 Reply Last reply
                        0
                        • J James R Twine

                          It may be faster to use loops, depending on the setup required for the recursive function call (stack frame setup, construction/destruction of any local objects, exception handling setup if used, etc.).  If your function uses poor coding habits, like using a CString instead of a local buffer for simple formatting, using the loop will very likely be faster than recursion, because you can eliminate the overhead of allocating/deallocating the memory for the string.  (You may not notice a difference when only calling the function 100 or so times, but when you start hitting the 1000+ ranges, you may.)    A (well-balanced) binary tree is likely to have less recursive demand, whereas a list is a linear structure so your recursion level would be equal to the number of items in the list.  Trees tend to have less depth when used/populated correctly.    Peace!

                          -=- James
                          Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                          See DeleteFXPFiles

                          L Offline
                          L Offline
                          Lord_Vader
                          wrote on last edited by
                          #12

                          Thanks,Specifically I am working on a graphics algorithm which uses a binary tree to draw triangles(which are the leafs) on the screen. Now if you consider that I would like to have ~60000 leafs displayed every frame using a recursive function, this is quite cumbersome.Additionally the tree may not be so balanced, but the maximum depth of the tree wont be larger than ~14.Thats why I asked.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups