Announcement

Collapse

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
See more
See less

gosubing between subroutines

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • gosubing between subroutines

    I'm curious about how PowerBasic does gosubs when you gosub from a subroutine or function to a line label that exists in the main program, or even in a different subroutine. This compiles okay, but is it kosher to do? Coming at this from a C point of view, this is the equivalent of using setjmp to mark a spot, jump to the line label, and then return using a longjmp. In C this works in the same stack contact, and can even be used to return to an older stack frame, but not in reverse, as the stack contexts would be invalidated (or corrupted). Here's a PB program to illustrate what I'm asking:

    Code:
    sub test_sub
       print "gosubing to part of the main program"
       gosub mainline
       print "I'm back."
    end sub
    
    a=5
    print "This is the main program"
    print "a =";a
    test_sub
    end
    
    mainline:
    print "This is the main program being called by the subroutine"
    print "This is a:";a
    return
    This code just seems like it ought to be illegal because it's calling down the stack and then returning! What if the code at mainline: made other subroutine calls, using the stack (which ought to really corrupt it when the gosub returns)? Does PB to some special work to turn mainline into a real subroutine with a special link to the main program's variable area?

    This behavior is odd. I've also notices pb3.2 lets me gosub between subroutines! This leads me to believe PB somehow makes gosubs be normal procedure calls.

    Can anyone shed some light on this behavior? I'll check my pb programmer's guide in the meantime...

    Michael


    ------------------

  • #2
    While the compiler does permit this to occur, it should be avoided unless you know exactly what you are doing! One of the main concerns for this type of code is that the state of the stack frame and local data storage cannot be accertained, etc. Use this technique at your own risk!

    For this reason, PowerBASIC's Windows compilers (PB/CC and PB/DLL) do not allow out-of-scope calls, such as you mention.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment

    Working...
    X