Qbasicnews.com

Full Version: Array as a parameter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've done this before, but it's not working this time. Here is my error:

Code:
game.bi(15) : error 57: Illegal specification, at parameter 1: list
Declare Sub EraseBricks(list() As PointType)
Are you sure PointType has been declared before this function declaration?
Woops :oops:. Now that i've fixed that... Are you not allowed to DIMension an array (more specifically a dynamic array) inside a SUB or Function?
You sure can:
Code:
sub foo(dynamicSize as integer)

    '/ declare fixed-size array
    const staticSize as integer = 420
    dim as integer staticBar(staticSize)
    
    '/ declare variable-size array
    dim as integer bar(dynamicSize)

end sub

staticSize must be a compile-time constant, of course (const, enum or literal).

Anonymous

since .16, you can also do this


Code:
sub foo()

  dim as integer ary()

  ...

  redim ary( 7162 )

  ...

end sub

before you could not specify "dimensionless" arrays inside of sub/funcs
Don't forget the arrays DIMed in subs/functions are created on the stack, and the stack is 1Mb by default.
If you need a bigger array either you make it shared, increase the stack size or use direct memory allocation.
You can also make a memory block act like an array.

Code:
Declare Function newInt(size as uinteger) as integer ptr
Declare Sub delete(pointr as integer ptr)
dim myArray as integer ptr
myArray = newInt(200)

for i = 0 to 200
   myArray[i] = RND * 1000
next

delete myArray

Function newInt(size as uinteger) as integer ptr
   return allocate(size * 4)
End Function

Sub delete(pointr as integer ptr)
   deallocate pointr
End Sub

Ah, dynamic allocation Smile
Be careful using literals for datatype size like that. You should always prefer sizeof or len. I prefer sizeof myself, simply because it seems more compile-time to me, as len is also a run-time function. But they both give compile-time results for var and datatype sizes.