Announcement

Collapse
No announcement yet.

Bug or Feature with Arrays?

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

  • Bug or Feature with Arrays?

    I have found a strange behavior.

    If i declare an empty Array and write code that try to access
    an element of that array, but have no code wich dimensions that
    array the compiler will not compile my code with error 461.
    Should it not be better a runtime error instead of a compiler error?

    Example

    Global MyArray() as MyType

    'Remove the comments in PBMain and the code will compile
    Function PBMain ....
    'Redim MyArray(0) as MyType
    'Erase MyArray
    Msgbox MyArray(1).MyTypeMember
    End Function

    ... a lot of other functions

    Regards,

    Torsten


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

  • #2
    Torte,

    Your correct. If you wish to access a dynamic array you will
    need to do it this way:

    Code:
    #compile exe
    global sample() as string
    
    function pbmain() as long
       redim sample(0 to 0)
       slickexamplesub sample()
       msgbox sample(1)
       msgbox sample(2)
    end function


    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

    Comment


    • #3
      Referencing an array outside of it's bounds at runtime is one of the best ways to trigger a GPF. Referencing an array that has been erased is another good way.

      PB/DLL and PB/CC do not support the DOS Basic "auto-dim" for undimensioned arrays.

      Essentially, the compiler detected that your array had not been dimensioned at all, and this would have caused a definite runtime failure. It correctly flagged the error.

      Note that unless you use #DEBUG ERROR ON, PowerBASIC does not validate array subscript values at run-time - such range validations are the responsibility of the programmer.

      In summary (and IMHO), I'd much rather find a problem with an undeclared array at compile-time rather than have a customer report a show-stopping run-time error from the field.




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

      Comment

      Working...
      X