dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] Exit function vs Return and stack overflow issue?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Posts
    65

    Resolved [RESOLVED] Exit function vs Return and stack overflow issue?

    Hello,

    Code 1:
    Public Function ABC() as Boolean
    ABC=True
    Exit function
    End Function

    Code 2:
    Public Function ABC() as Boolean
    Return True
    End Function

    Question:
    I know that using "Exit function" causes stack overflow due to abnormal exit from the function. When you call such a function (code 1) many times you get stack overflow error message because residual allocations resides in stack region of memory.

    What about return from a function (Code 2). Is this legal exit from the function? Does it also cause stack overflow when it is called enough times?

    Thank you.

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    660

    Re: Exit function vs Return and stack overflow issue?

    This has nothing to do with stack overflow. The first example is the clumsy way of returning a function result that is compatible with VB6. The second, using 'Return' is what everyone in this century should be doing.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,314

    Re: Exit function vs Return and stack overflow issue?

    I had never heard that the first causes a stack overflow, and don't see why that would be the case. When the function exits, the stack frame should be cleared up. That may be because this is a simple example. You CAN get stack overflows if you aren't just exiting the function, so you may have encountered that in a similar, though slightly and significantly different fashion, where one function called to another rather than exiting.

    Otherwise, I agree with what David said: The first version is how VB6 handled function returns. The second version is the way pretty nearly EVERY other language handles function returns.

    EDIT: Just to try that out, I called the function you said would cause a stack overflow in a loop about 84 billion times (at which point I got bored and quit), and never got a stack overflow. This system has 8 GB of RAM, so even if it was leaving only a byte each time on the stack, I would have exhausted the memory 10 times over. So, that will NOT cause a stack overflow the way it is written.
    Last edited by Shaggy Hiker; Mar 17th, 2019 at 10:42 AM.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Posts
    65

    Re: Exit function vs Return and stack overflow issue?

    Thank you for clarification.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width