Richard wrote:I assumed the array already existed and Ircvs wanted to use pointers or assembly code for access.
About this, a short example to enlighten how to access to array elements, but from pointer index operator.
3D array:
- classic access by array indexes
- access by pointer operator with 1 index
- access by pointer operator with 2 indexes
- access by pointer operator with 3 indexes
Code: Select all
#Macro Print_all_elements_with_comment(VARijk, COMMENT)
Print "Print from " & #VARijk;
If COMMENT <> "" then
Print " " & COMMENT;
End If
Print
For j As Integer = 0 To Uj
For i As Integer = 0 To Ui
For k As Integer = 0 To Uk
Print Using "####"; VARijk;
Next k
Print " ";
Next i
Print
Next j
Print
#Endmacro
Dim Ui As Integer = 2
Dim Uj As Integer = 3
Dim Uk As Integer = 4
Dim array(Ui, Uj, Uk) As Integer
For i As Integer = 0 To Ui
For j As Integer = 0 To Uj
For k As Integer = 0 To Uk
array(i, j, k) = i * 100 + j * 10 + k
Next k
Next j
Next i
Print_all_elements_with_comment(array(i,j,k), "directly with array indexes")
Dim p As Integer Ptr
p = @array(0, 0, 0)
Print_all_elements_with_comment(p[(i*(Uj+1)+j)*(Uk+1)+k], "by pointer + 1 index, with p=@array(0,0,0)")
Dim pp As Integer Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr))
For i As Integer = 0 To Ui
pp[i] = @array(i, 0, 0)
Next i
Print_all_elements_with_comment(pp[i][j*(Uk+1)+k], "by pointer + 2 indexes, with pp[i]=@array(i,0,0)")
Dim ppp As Integer Ptr Ptr Ptr = Callocate(Ui + 1, Sizeof(Integer Ptr Ptr))
For i As Integer = 0 To Ui
ppp[i] = Callocate(Uj + 1, Sizeof(Integer Ptr))
For j As Integer = 0 To Uj
ppp[i][j] = @array(i, j, 0)
Next j
Next i
Print_all_elements_with_comment(ppp[i][j][k], "by pointer + 3 indexes, with ppp[i][j]=@array(i,j,0)")
Sleep
Deallocate(p)
For i As Integer = 0 To Ui
Deallocate(pp[i])
Next i
Deallocate(pp)
For i As Integer = 0 To Ui
For j As Integer = 0 To Uj
Deallocate(ppp[i][j])
Next j
Deallocate(ppp[i])
Next i
Deallocate(ppp)
Code: Select all
Print from array(i,j,k) directly with array indexes
0 1 2 3 4 100 101 102 103 104 200 201 202 203 204
10 11 12 13 14 110 111 112 113 114 210 211 212 213 214
20 21 22 23 24 120 121 122 123 124 220 221 222 223 224
30 31 32 33 34 130 131 132 133 134 230 231 232 233 234
Print from p[(i*(Uj+1)+j)*(Uk+1)+k] by pointer + 1 index, with p=@array(0,0,0)
0 1 2 3 4 100 101 102 103 104 200 201 202 203 204
10 11 12 13 14 110 111 112 113 114 210 211 212 213 214
20 21 22 23 24 120 121 122 123 124 220 221 222 223 224
30 31 32 33 34 130 131 132 133 134 230 231 232 233 234
Print from pp[i][j*(Uk+1)+k] by pointer + 2 indexes, with pp[i]=@array(i,0,0)
0 1 2 3 4 100 101 102 103 104 200 201 202 203 204
10 11 12 13 14 110 111 112 113 114 210 211 212 213 214
20 21 22 23 24 120 121 122 123 124 220 221 222 223 224
30 31 32 33 34 130 131 132 133 134 230 231 232 233 234
Print from ppp[i][j][k] by pointer + 3 indexes, with ppp[i][j]=@array(i,j,0)
0 1 2 3 4 100 101 102 103 104 200 201 202 203 204
10 11 12 13 14 110 111 112 113 114 210 211 212 213 214
20 21 22 23 24 120 121 122 123 124 220 221 222 223 224
30 31 32 33 34 130 131 132 133 134 230 231 232 233 234