VB6之多维数组中元素在内存中的排列情况
1 Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long) 2 3 4 'code by lichmama from cnblogs.com 5 Private Sub Form_Load() 6 Dim a(2) As Byte 7 Dim b(2, 2) As Byte 8 Dim c(2, 2, 2) As Byte 9 10 Debug.Print "一维数组:" 11 Debug.Print VarPtr(a(0)), VarPtr(a(1)), VarPtr(a(2)) 12 '数组元素在内存中的排列情况: 13 'a[0]|a[1]|a[2] 14 15 Debug.Print "二维数组:" 16 Debug.Print VarPtr(b(0, 0)), VarPtr(b(1, 0)), VarPtr(b(2, 0)) 17 Debug.Print VarPtr(b(0, 1)), VarPtr(b(1, 1)), VarPtr(b(2, 1)) 18 Debug.Print VarPtr(b(0, 2)), VarPtr(b(1, 2)), VarPtr(b(2, 2)) 19 '数组元素在内存中的排列情况: 20 'b[0,0]|b[1,0]|b[2,0]-->b[0,1]|b[1,1]|b[2,1]-->b[0,2]|b[1,2]|b[2,2] 21 22 Debug.Print "三维数组:" 23 Debug.Print VarPtr(c(0, 0, 0)), VarPtr(c(1, 0, 0)), VarPtr(c(2, 0, 0)) 24 Debug.Print VarPtr(c(0, 1, 0)), VarPtr(c(1, 1, 0)), VarPtr(c(2, 1, 0)) 25 Debug.Print VarPtr(c(0, 2, 0)), VarPtr(c(1, 2, 0)), VarPtr(c(2, 2, 0)) 26 Debug.Print VarPtr(c(0, 0, 1)), VarPtr(c(1, 0, 1)), VarPtr(c(2, 0, 1)) 27 Debug.Print VarPtr(c(0, 1, 1)), VarPtr(c(1, 1, 1)), VarPtr(c(2, 1, 1)) 28 Debug.Print VarPtr(c(0, 2, 1)), VarPtr(c(1, 2, 1)), VarPtr(c(2, 2, 1)) 29 Debug.Print VarPtr(c(0, 0, 2)), VarPtr(c(1, 0, 2)), VarPtr(c(2, 0, 2)) 30 Debug.Print VarPtr(c(0, 1, 2)), VarPtr(c(1, 1, 2)), VarPtr(c(2, 1, 2)) 31 Debug.Print VarPtr(c(0, 2, 2)), VarPtr(c(1, 2, 2)), VarPtr(c(2, 2, 2)) 32 '数组元素在内存中的排列情况: 33 'c[0,0,0]|c[1,0,0]|c[2,0,0]-->c[0,1,0]|c[1,1,0]|c[2,1,0]-->c[0,2,0]|c[1,2,0]|c[2,2,0] 34 '-->c[0,0,1]|c[1,0,1]|c[2,0,1]-->c[0,1,1]|c[1,1,1]|c[2,1,1]-->c[0,2,1]|c[1,2,1]|c[2,2,1] 35 '-->c[0,0,2]|c[1,0,2]|c[2,0,2]-->c[0,1,2]|c[1,1,2]|c[2,1,2]-->c[0,2,2]|c[1,2,2]|c[2,2,2] 36 37 '我们习惯逻辑上这么给数组赋值,字面上像是b()="lhmimaca!" 38 b(0, 0) = &H6C: b(0, 1) = &H68: b(0, 2) = &H6D 39 b(1, 0) = &H69: b(1, 1) = &H6D: b(1, 2) = &H61 40 b(2, 0) = &H63: b(2, 1) = &H61: b(2, 2) = &H21 41 42 Dim text As String 43 text = String(9, " ") 44 Call RtlMoveMemory(ByVal StrPtr(text), ByVal VarPtr(b(0, 0)), 9) 45 Debug.Print "但,其实结果是这样的:"; 46 Debug.Print StrConv(text, vbUnicode) 47 End Sub
看结果说话(内存地址打印):
一维数组: 2031464 2031465 2031466 二维数组: 2048432 2048433 2048434 2048435 2048436 2048437 2048438 2048439 2048440 三维数组: 2059256 2059257 2059258 2059259 2059260 2059261 2059262 2059263 2059264 2059265 2059266 2059267 2059268 2059269 2059270 2059271 2059272 2059273 2059274 2059275 2059276 2059277 2059278 2059279 2059280 2059281 2059282 但,其实结果是这样的:lichmama!