Menu

Scope and life-cycle of variables

joe rocci
2014-06-20
2014-08-02
  • joe rocci

    joe rocci - 2014-06-20

    Has anything been written anywhere about the scope and life-cycle of GCB variables? Do variable-to-ram assignments ever get destroyed or recycled? Is there a way to free up ram by destroying variable-to-ram assignments? Do variables have differences in scope depending on where they're assigned/used in the program?

    Joe

     
  • Anobium

    Anobium - 2014-06-21

    I have not read or found anything with respect this subject.

    From my knowledge.

    Scope - every variable is global.
    Life cycle - once a variable is defined as used it persists.
    Aliasing - You can use aliasing to reduce memory usage but as a all variables are global you must be careful.

    Anyone else got anything to add?

     
  • Hugh Considine

    Hugh Considine - 2014-06-21

    That's pretty much it! The only thing to add, variables are global, but if they are defined inside a particular subroutine then their type isn't. Some example code:

    Dim MainVar As Byte
    Dim OtherVar As Word
    
    MainVar = 128
    OtherVar = 514
    
    DemoSub
    'At this point:
    'MainVar is a byte, value 128
    'OtherVar is a word, value 514
    'Counter is a byte, value 2
    '(Byte is default type, but location shared with that of Counter in DemoSub. High byte ignored)
    
    Sub DemoSub
        Dim Counter As Word
        Counter = 2050
        'At this point:
        'MainVar and OtherVar as byte and word, as in main routine
        'Counter is a word, value 2050
    End Sub
    

    Things have been done this way to minimise the size of the generated assembly code. At some point, variables local to particular subroutines may be implemented. If this happens, there will be some #config option to enable/disable this. As a general guide though, it's best to define any shared variables near the start of the program for easier readability.

     
  • Chuck Hellebuyck

    Just to clarify, when you say byte is default type, that means the
    Dim MainVar As Byte is unnecessary.
    MainVar = 128 takes care of the Dim portion automatically, correct?

    But I don't understand the counter is 2 as it was never used prior and once the subroutine DemoSub defines Counter as a word, isn't it a word variable throughout?

     
  • Hugh Considine

    Hugh Considine - 2014-06-21

    Yes, technically there's no need to Dim anything as a byte, that happens automatically. Counter is 2 because the variable is global but the type isn't. In DemoSub, it's a word. But anywhere else, it's a byte unless otherwise specified. If read in the main routine, it will be treated as a byte, and only the low 8 bits will be returned. The low 8 bits of 2050 are 2.

    (The global variable, local type idea is admittedly a bit silly now. It made sense in 2006 when GCBASIC only supported 16F chips and its 16 bit arithmetic support was shaky at best, but maybe there is a better way now?)

     
  • joe rocci

    joe rocci - 2014-06-23

    Thanks for the reply Hugh.

    I still have some questions. If a variable is DIM'd in a Sub, is RAM reserved for it according to the size it was DIM'd to, eg, 2 bytes of RAM for a Word? If that variable is then used outside the Sub, it's treated as a Byte, but are there still 2 bytes reserved for it? This was what triggered my more general question about the life-cycle of variables and associated RAM assignments.

    What I'm discovering is that, to avoid unexpected behavior, I should try to DIM all my variables at the beginning of the program. Are there any reasons why/when variables should be DIM'd inside the Sub they're used in?

    Joe

     
  • Hugh Considine

    Hugh Considine - 2014-06-23

    The main reason for keeping the type inside the subroutine was for this scenario
    - A subroutine uses a temporary variable, say of type byte, and relies on it overflowing
    - Another subroutine uses a temporary variable of the same name, but of word type
    - If the first subroutine is already in the program, and then the second one is added, the behaviour of the first one won't change at all due to the addition of the second one.

    But because the compiler doesn't currently have any concept of local variables, it needs to put all variables with the same name into the same RAM location.

     
  • joe rocci

    joe rocci - 2014-07-31

    Just to clarify though...

    I'm finding that if I DIM a variable inside a sub, that variable isn't visible to code outside the sub. Is that correct? Does this mean I can reuse the same variable name in different subs so long as the variable name is declared in each sub it's used in?

     
  • Anobium

    Anobium - 2014-08-01

    @hugh. Can you answer?

     
  • Hugh Considine

    Hugh Considine - 2014-08-02

    If there are two variables with the same name, they will be placed in the same memory location. So you can reuse the same name in two subs, and you can make the variables different types, but writing to the variable in one sub will overwrite the value from the other sub.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.