chance of stack overflow

Discussion in 'C Programming' started by asit, Dec 2, 2007.

  1. asit

    asit Guest

    we kno during call of a subroutine, the address of next
    instruction( from which the execution is supposed to start after the
    subroutine is executed ) is stored in stack. in case of recursion
    (e.g. we are sorting an array having length one million using heap
    sort), we have to call a particular subroutine thousands time. Is
    their any chance of stack overflow ?? if yes how can we avoid ??

    here we have no choice other than subroutine..
    asit, Dec 2, 2007
    #1
    1. Advertising

  2. asit said:

    > we kno during call of a subroutine, the address of next
    > instruction( from which the execution is supposed to start after the
    > subroutine is executed ) is stored in stack.


    We don't actually know that, because the C Standard doesn't guarantee it,
    but it is certainly true that some systems work in that way.

    > in case of recursion
    > (e.g. we are sorting an array having length one million using heap
    > sort), we have to call a particular subroutine thousands time. Is
    > their any chance of stack overflow ??


    It's unlikely. If you're sorting a million items in a recursive O(n log n)
    algorithm, you're unlikely to have to recurse more than twenty calls deep,
    and that's really no big deal. (2^20 == 1048576)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Dec 2, 2007
    #2
    1. Advertising

  3. asit

    asit Guest

    then what is the best recursion procedure..if we have to sort a
    million integers
    asit, Dec 2, 2007
    #3
  4. asit said:

    > then what is the best recursion procedure..if we have to sort a
    > million integers


    It depends on the range of the integers. For example, if the inputs were
    all in the range 0 to 9 - single digits - I would give one answer, and if
    the inputs were bignums of arbitrary length, I'd give a very different
    answer. And if, as is likely, the truth is somewhere between those two
    outliers, I'd give a different answer again.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Dec 2, 2007
    #4
  5. asit wrote:
    > we kno during call of a subroutine, the address of next
    > instruction( from which the execution is supposed to start after the
    > subroutine is executed ) is stored in stack.


    We know (note spelling) no such thing. There are many ways for
    subroutines to be supplied with the location to which returns should
    lead, and many ways for subroutines to use such information.

    Any question based upon your false premise is useless. However,

    > in case of recursion
    > (e.g. we are sorting an array having length one million using heap
    > sort), we have to call a particular subroutine thousands time. Is
    > their any chance of stack overflow ?? if yes how can we avoid ??


    Unless you somehow have access to an infinite memory machine, it is
    obvious that any unrestrained use of a stack carries with it the chance
    of overflowing that stack. It doesn't matter what use you put that
    stack to. There is no automatic way of avoiding violating the limits of
    any region of memory. As to which precautions you should take, that
    is obviously intimately bound to your implementation. Knowledge of your
    implementation and the skills to use that knowledge have nothing to do
    with the C programming language and are off-topic in <news:comp.lang.c>
    Martin Ambuhl, Dec 2, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=

    Stack overflow exception

    =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=, Apr 20, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,244
    Rick Spiewak
    Apr 22, 2004
  2. Mr m?ll
    Replies:
    2
    Views:
    1,399
    Mr m?ll
    Oct 16, 2004
  3. Replies:
    0
    Views:
    496
  4. Replies:
    0
    Views:
    401
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    258
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page