3維陣列的記憶體配置跟用1維來模擬3維陣列一樣,簡單用程式來驗證:
/**
Theme: 3-d Array and Memory Address
Compiler: Dev C++ 4.9.9.2
Date: 100/01/07
Author: ShengWen
Blog: https://cg2010studio.wordpress.com/
*/
#include<iostream>
using namespace std;
const int SIZE=3;
int main(){
int data[SIZE][SIZE][SIZE];
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
cout<<&data[i][j][k]<<' ';
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
cout<<*(*(data+i)+j)+k<<' ';
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}
輸出結果:
0x23ff00 0x23ff04 0x23ff08
0x23ff0c 0x23ff10 0x23ff14
0x23ff18 0x23ff1c 0x23ff20
0x23ff24 0x23ff28 0x23ff2c
0x23ff30 0x23ff34 0x23ff38
0x23ff3c 0x23ff40 0x23ff44
0x23ff48 0x23ff4c 0x23ff50
0x23ff54 0x23ff58 0x23ff5c
0x23ff60 0x23ff64 0x23ff68
0x23ff00 0x23ff04 0x23ff08
0x23ff0c 0x23ff10 0x23ff14
0x23ff18 0x23ff1c 0x23ff20
0x23ff24 0x23ff28 0x23ff2c
0x23ff30 0x23ff34 0x23ff38
0x23ff3c 0x23ff40 0x23ff44
0x23ff48 0x23ff4c 0x23ff50
0x23ff54 0x23ff58 0x23ff5c
0x23ff60 0x23ff64 0x23ff68
請按任意鍵繼續 . . .
可以看到,雖然是3維,可是記憶體是連續的。程式用array index和pointer index來輸出,得到得結果是一樣的。
隨意留個言吧:)~