Will this work?

BlitzMax Forums/BlitzMax Programming/Will this work?

TAS(Posted 2016) [#1]
I am assuming arrays fill a continuous block of memory. Are ther any exceptions?

Local Data[10,10,5]
Clear(data[0,0,0],data.length,66)
Print data[0,0,0]
Print data[9,9,0]
Print data[9,9,4]

Function Clear(data0:Int Var,length,newValue=0)
  'create pointer to first entry in integer array
  Local int_Ptr:Int Ptr = Varptr Data0
  size=length*4
  'create bank in same memory area as array
  Local DataBank:TBank=CreateStaticBank(int_Ptr,size)
  'set array to new value
  For i=0 To length-1
      PokeInt DataBank,i*4,newValue
  Next
End Function



col(Posted 2016) [#2]
In the scenarios that use byte,short,int,float and long, then as long as your 'size = length * datatype_size' is using the correct 'datatype_size' then I would expect it to work.

It won't work on an array of Objects though, which would be Types,Strings,Arrays and err... anything else based on Object :p

It looks kind of naughty though. I assume you have a good reason to use the indirection? Otherwise what's wrong with using...



Derron(Posted 2016) [#3]
Compile Error: Incorrect number of array dimensions

-> you access a multi-dimensional array (or "array of arrays" - never sure in which way BMax implements it) in a "single-dimension"-way



bye
Ron


col(Posted 2016) [#4]
I can't remember if I used Ng or legacy for that example, either way it worked here, so one of them is wrong :/


TAS(Posted 2016) [#5]
It looks kind of naughty though. I assume you have a good reason to use the indirection? Otherwise what's wrong with using...


I did this way so the function will work with integer arrays with any number of dimensions.


grable(Posted 2016) [#6]
Why so convoluted?

Heres my take on it, though it only works for Int arrays as is.
SuperStrict

Local a:Int[2,2,2]
a[0,0,0] = 1
a[1,1,1] = 1

'MemClear a, GetArrayLength(a) * 4 ' probably the fastest for 0
Clear a, GetArrayLength(a)

For Local z:Int = 0 Until a.Dimensions()[2]
	For Local y:Int = 0 Until a.Dimensions()[1]
		For Local x:Int = 0 Until a.Dimensions()[0]
			Print a[x,y,z]
		Next
	Next
Next

Function GetArrayLength:Int( o:Object)
	Local a:Int[] = Int[](o)
	If a Then
		Local length:Int = 1
		For Local l:Int = EachIn a.Dimensions()
			length :* l
		Next
		Return length
	EndIf
	Return 0
EndFunction

Function Clear( p:Int Ptr, length:Int, val:Int = 0)
	Local e:Int Ptr = p + length
	While p < e
		p[0] = val
		p :+ 1
	Wend
EndFunction