Qbasicnews.com
Array as a parameter? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Array as a parameter? (/thread-9246.html)



Array as a parameter? - Torahteen - 05-02-2006

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)



Array as a parameter? - stylin - 05-02-2006

Are you sure PointType has been declared before this function declaration?


Array as a parameter? - Torahteen - 05-02-2006

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?


Array as a parameter? - stylin - 05-02-2006

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).


Array as a parameter? - Anonymous - 05-02-2006

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


Array as a parameter? - Antoni Gual - 05-02-2006

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.


Array as a parameter? - wallace - 05-03-2006

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


Array as a parameter? - stylin - 05-03-2006

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.