Array questions. Deleting/renaming?

Blitz3D Forums/Blitz3D Beginners Area/Array questions. Deleting/renaming?

Galdy(Posted 2011) [#1]
How do you delete an array so it is not using up memory? Also, can is it possible to rename an array and without losing it's contents, or do you have to dim a new array if you want to store the data of the old array you are deleting?

Galdy


etxtra(Posted 2011) [#2]
hi @Galdy, I'm new to blitz3D but I have a good experience with Basic programming (QBasic, DarkBasic pro... yes I've used these) :
-from what I can see in the documentation you can't delete an array nor a variable ; note that in other Basic languages there is an "UnDim" function...
-you can't rename an array in Blitz3D and if you want to change its size you may lose the data. You may create a new array and then copy data from the first to the new...
-this is what I've understood from the documentation...
hope it helps.

Last edited 2011


Yasha(Posted 2011) [#3]
First thing: there are two kinds of array in Blitz3D: function-style, and C-style. They're completely different animals and don't really interact in any way.
Dim myFStyleArray(100)
Global myCStyleArray[100]


Function-style (or Basic-style, or Dim-style - I call them function-style because they look like function invocations) arrays:
- are always global
- can be resized
- can never be used as first-class values

This means that that array lives in that variable, can't be put in another variable with an assignment expression or as a function parameter, and they're visible throughout the entire program (same as globals and function definitions). If you want to expand or shrink the array (saving memory might well be one reason) you can change its size by calling Dim a second time. You can Dim or re-Dim an array to have zero elements when you're done with it. What you can't do is extend an array while using it, as the current elements will be lost.

C-style arrays are different. Anywhere a variable can be declared, a C-style array can be declared simply by following the name with the desired size in square brackets. These can have Local, Global or Field scope, and persist as long as the name they're attached to does (global ones persist forever, local and field arrays are created and all elements zeroed when either the function scope is entered or the object is created with New). So obviously local and field arrays can be deleted at known times.

The downside is that these arrays must have a compile-time constant size; there is no way to extend or shorten them while using them. You shouldn't need this functionality anyway. They can also only have one dimension.

C-style arrays still can't be put in other variables with an assignment, but they can be passed as function parameters. Define the parameter as normal, and add square brackets with the (correct) size after the name, and you can then pass the array in (using the name without an index). The array is passed by reference and the parameter name refers to the same array object; any changes will affect the array in its home location.

Simple example:
Function PrintArray(arr[10])
	Local i
	For i = 1 To 10
		Write " " + arr[i]
	Next
	Print ""
End Function

Function SquareArray(arr[10])
	Local i
	For i = 1 To 10
		arr[i] = arr[i] * arr[i]
	Next
End Function

Local myArray[10], i

For i=1 To 10
	myArray[i] = i
Next

PrintArray(myArray)
SquareArray(myArray)
PrintArray(myArray)

WaitKey
End


Last edited 2011


Galdy(Posted 2011) [#4]
Thanks for the input guys. As it turns out i didn't need to do what i was proposing. For some retarded reason I was trying to make the array name the same as the file name it would be written to. LOL. Newben out. Totaly not nec. Never the less the question/answers did clear things up about arrays for me though. Thank you.


Kryzon(Posted 2011) [#5]
Not only for you but for the many others that will eventually search this subject :)