Question on Arrays and Memory and Question on Type

BlitzMax Forums/BlitzMax Beginners Area/Question on Arrays and Memory and Question on Type

Smurftra(Posted 2006) [#1]
Hi all

I have some questions regardings arrays and how the memory is used and also on types.

1) I'm using an array to store objects of a type i did myself (The type only has fields, no methods, its basicaly a C Struct or VB Type, for reference)

I want to remove element X from my array. I do this by looping the array from X to the end and then repositioning the value (So if i had 1,2,3,4 and want to remove 2, i copy 3 over 2, then 4 over 3, so i'm left with 1,3,4,4)

Then i used this:

Array = Array[..Array.Length-1]

It works. Now to my question: What happens with the last element of the array? Is it automaticaly removed from memory? If i redim the array again to Length+1, will the new element already be set to 4?

(I do not want to use a list because i want to be able to calculate mathematicaly what element i want to read/write. (Array[X*Y].Field = 0). So please dont tell me i can fix this with a list cuz it wont work for my final goal)

2) Ok, maybe someone who coded some VB can help me on my question about Types.

In VB, you can have a Type, which is like the types in BlitzMax but without methods/functions.

Then you can have classes, which is like BlitzMax's Type when there are methods in it.

So given these Types:

Type One
Field X:Int
Field Y:Int
End Type

Type Two
Field O:One[10]

Method Swap (ID1:Int, ID2:Int)
Local Temp:One

Temp = O(ID1)
O(ID1) = O(ID2)
O(ID2) = Temp
End Method
End Type


ok, so my method, Swap, what is it doing?

Is it simply swapping pointers, or is it actually swapping the values of the fields X and Y of my type One?

I'll rephrase because i know my english is not that good and want to make sure i'm clear:

In VB, a type without methods is like an Int, if i say A = B, the values changes, it doesnt make A point on B. That is what i want to acheive.

So, will my code work, or should i use:

Method Swap (ID1:Int, ID2:Int)
Local Temp:One

Temp.X = O(ID1).X
Temp.Y = O(ID1).Y

O(ID1).X = O(ID2).X
O(ID1).Y = O(ID2).Y

O(ID2).X = Temp.X
O(ID2).Y = Temp.Y

End Method


[EDIT]

Forgot to add this: if i use Array.Length alot, is it like a variable or it calculates the length each time? (Should i store array.length in a local variable if i'm gonna use it more than once or would this cost the same on the cpu/whateverelse)

Thanks in advance for the help


Floyd(Posted 2006) [#2]
I think you misunderstand what slicing does.
A slice consists of new things, which are copies of old things.

Consider the slices in this code.
Local a[] = [ 111,222,333,444,555 ]
Local b[]

b = a[..3]  ' a[..3] is a new array, built from copies of a[0],a[1],a[2]

b[0] = 321

Print
Print a[0]  ' check that a[] and b[] are different arrays
Print b[0]

a = a[..3]  ' a[..3] is another new array, just as with b = a[..3]

' At this point there are no references to the original five elements 
' of a[] and they are available for garbage collection.



FlameDuck(Posted 2006) [#3]
I want to remove element X from my array. I do this by looping the array from X to the end and then repositioning the value (So if i had 1,2,3,4 and want to remove 2, i copy 3 over 2, then 4 over 3, so i'm left with 1,3,4,4)
If you don't need the objects sorted, a quicker way is to swap the object you want to get rid of, with the last element in the array.

Is it simply swapping pointers, or is it actually swapping the values of the fields X and Y of my type One?
It is simply swaping the references.


Smurftra(Posted 2006) [#4]
thanks for the answers

Floyd: Ok i understand now, but i think the blitz manual needs an overhaul if that is the case because it says i can redim arrays using slices

i mean, here's an example straight from the help:

Local a[200] 'initialize a[] to 200 elements
(...)
a=a[..200] 'resize a[] to 200 elements


so i though blitz was 'intelligent' and understood that if i use the same variable (a) i want to redim and i use a new variable (b=a[..200]) i want a copy.

So basically i'm doing it right, and the memory will be cleared at one point by blitz by itself since flushmem doesnt exist anymore?


Flameduck: thanks for the answer about the pointers. In my case i need the elements sorted.

Would still like an answer to this:

if i use Array.Length alot, is it like a variable or it calculates the length each time? (Should i store array.length in a local variable if i'm gonna use it more than once or would this cost the same on the cpu/whateverelse)

again thanks for the help, much appreciated.


ImaginaryHuman(Posted 2006) [#5]
the array length would not change unless you were making a new one, so probably it doesn't require a calculation more than one time per creation?


Dreamora(Posted 2006) [#6]
you can use array.dimensions()[0] to get its actual size (or [i] for the i-th dimension), just in case Length is not update on slicing.


And yes, the BM garbagecollector is now automatic and will clear unreferenced elements (this does not mean it is freed when it falls out of the array automatically. As long as it is used somewhere else it will stay active)