Pointer to Part of an Array

BlitzMax Forums/BlitzMax Beginners Area/Pointer to Part of an Array

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Try this test program:
Local OneD:Int[1024*1024]	'4 megabytes of Int's
Print "Testing 4MB memory one-dimensional array"
'Test  continuity
Local Last:Int=Int(Varptr(OneD[0]))
Local Current:Int
Local Cont:Int=True
For Local Count:Int=1 To (1024*1024)-1
	Current=Int(Varptr(OneD[Count]))
	If Current<>Last+4
		Print "Last: "+Last+", Current: "+Current+", Discontinuous Memory!"
		Cont=False	
		WaitKey
	EndIf
	Last=Current
Next
If Cont=True Then Print "Memory was continuous!" Else Print "Memory was not continuous!"
Print "~nTesting two-dimensional array 1024x1024 (4 MB)"
Local TwoD:Int[1024,1024]	'4 megabytes of Int's
'Test  continuity
For Count=1 To 1024
	If Varptr(TwoD[Count-1,1023])<>Varptr(TwoD[Count,0])-4 Then Print "Separate memory for everything in ["+Count+",x]"
Next
Local Diff:Int=False
For Count=1 To 1024
	If Varptr(TwoD[0,Count-1])<>Varptr(TwoD[0,Count])-4
		Print "Separate memory within [0,"+Count+"]"
		Diff=True
	EndIf
Next
If Diff=True Then Print "Memory within dimension [0,x] is discontinuous" Else Print "Memory within dimension [0,x] is all continuous"

If this is correct and I'm interpreting it right, it suggests that:

a) For a one dimensional array, everything within that one dimension is in contiguous memory.
b) In a two-dimensional array, each of the first dimension elements points to a whole separate block of contigous memory which holds all of its children. So array[0,0] array[0,1] array[0,2] array [0,3] are all contigous, but array[0,0] array[1,0] and array[2,0] are separate memory areas. If you have more dimensions than 2 the same still applies, for each level, if there is a level below it, all of the contents of the below level are in a contiguous block of memory refereced by the current element in the current level.

The only thing this did not test is fragmented memory and whether this introduces a split.