[C++] 數組的地址

從結果來看,棧上的二維數組全是連續的,即arr[0][9] 到 arr[1][0] 也是連續的,這點應該沒有什麼爭議。
對於動態分配的數組來說,先將二維指針分配10個一維指針,此時創建了10個指針對象,這十個指針對象各自有自己的地址,我們用&讀取出來,指針對象的值也是一個地址,我們直接可以用test[i]訪問,指針對象的值的地址中的值,我們可以用*號訪問。

ptr of test[9]:0x2593bf8 value of test[9] itself:0x7d6270 value of test[9]:0 ptr of test[9][0]:0x7d6270 value of test[9][0]:0

可以看到,value of test[9] itself:0x7d6270 和 ptr of test[9][0]:0x7d6270是一樣的,也就是說,test[9]這個指針對象的值就是test[9][0]的地址。也因此,從test[0][9] 到 test[1][0]不是連續的,因爲test[1][0]是由test[1]指向的一個地址,和test[0]沒有關係。
從代碼裏還可以複習一下對象的地址(&),值(直接寫)和值作爲地址時指向的值(*)這三者之間的關係和獲取方法。

#include<iostream>
#include <cstring>
using namespace std;
int main()
{
	cout<<"stack:"<<endl;
	int arr[10][10] = {0};
	cout<<"sizeof(arr): "<<sizeof(arr)<<endl;
	for(int i = 0;i<10;i++)
	{
		cout<<"ptr of arr["<<i<<"]:"<<&arr[i]<<" ";
		cout<<"value of test["<<i<<"] itself:"<<hex<<arr[i]<<" ";
		cout<<"value of arr["<<i<<"]:"<<hex<<*arr[i]<<" ";
		for(int j = 0;j<10;j++)
		{
			cout<<"ptr of arr["<<i<<"]["<<j<<"]:"<<&arr[i][j]<<" ";
			cout<<"value of arr["<<i<<"]["<<j<<"]:"<<arr[i][j]<<endl;
		}
		cout<<endl;
	}
	int **test = new int *[10];
	cout<<"heap:"<<endl;
	for(int i = 0;i<10;i++)
		{
			cout<<"ptr of test["<<i<<"]"<<&test[i]<<endl;
		}
	cout<<"sizeof(test),(on a 64-bit compiler): "<<sizeof(test)<<endl;
	for(int i = 0;i<10;i++)
	{
		test[i] = new int [10];
		memset(test[i],0,10*sizeof(int));
	}
	//memset(test,0,100*sizeof(int));
	for(int i = 0;i<10;i++)
	{
		cout<<"ptr of test["<<i<<"]:"<<&test[i]<<" ";
		cout<<"value of test["<<i<<"] itself:"<<hex<<test[i]<<" ";
		cout<<"value of test["<<i<<"]:"<<hex<<*test[i]<<" ";
		for(int j = 0;j<10;j++)
		{
			cout<<"ptr of test["<<i<<"]["<<j<<"]:"<<&test[i][j]<<" ";
			cout<<"value of test["<<i<<"]["<<j<<"]:"<<test[i][j]<<endl;
		}
		cout<<endl;
	}
	for(int i = 0;i<10;i++)
	{
		delete [] test[i];
	}
	delete [] test;
	return 0;
}
***

stack:
sizeof(arr): 400
ptr of arr[0]:0x61fc50 value of test[0] itself:0x61fc50 value of arr[0]:0 ptr of arr[0][0]:0x61fc50 value of arr[0][0]:0
ptr of arr[0][1]:0x61fc54 value of arr[0][1]:0
ptr of arr[0][2]:0x61fc58 value of arr[0][2]:0
ptr of arr[0][3]:0x61fc5c value of arr[0][3]:0
ptr of arr[0][4]:0x61fc60 value of arr[0][4]:0
ptr of arr[0][5]:0x61fc64 value of arr[0][5]:0
ptr of arr[0][6]:0x61fc68 value of arr[0][6]:0
ptr of arr[0][7]:0x61fc6c value of arr[0][7]:0
ptr of arr[0][8]:0x61fc70 value of arr[0][8]:0
ptr of arr[0][9]:0x61fc74 value of arr[0][9]:0

ptr of arr[1]:0x61fc78 value of test[1] itself:0x61fc78 value of arr[1]:0 ptr of arr[1][0]:0x61fc78 value of arr[1][0]:0
ptr of arr[1][1]:0x61fc7c value of arr[1][1]:0
ptr of arr[1][2]:0x61fc80 value of arr[1][2]:0
ptr of arr[1][3]:0x61fc84 value of arr[1][3]:0
ptr of arr[1][4]:0x61fc88 value of arr[1][4]:0
ptr of arr[1][5]:0x61fc8c value of arr[1][5]:0
ptr of arr[1][6]:0x61fc90 value of arr[1][6]:0
ptr of arr[1][7]:0x61fc94 value of arr[1][7]:0
ptr of arr[1][8]:0x61fc98 value of arr[1][8]:0
ptr of arr[1][9]:0x61fc9c value of arr[1][9]:0

ptr of arr[2]:0x61fca0 value of test[2] itself:0x61fca0 value of arr[2]:0 ptr of arr[2][0]:0x61fca0 value of arr[2][0]:0
ptr of arr[2][1]:0x61fca4 value of arr[2][1]:0
ptr of arr[2][2]:0x61fca8 value of arr[2][2]:0
ptr of arr[2][3]:0x61fcac value of arr[2][3]:0
ptr of arr[2][4]:0x61fcb0 value of arr[2][4]:0
ptr of arr[2][5]:0x61fcb4 value of arr[2][5]:0
ptr of arr[2][6]:0x61fcb8 value of arr[2][6]:0
ptr of arr[2][7]:0x61fcbc value of arr[2][7]:0
ptr of arr[2][8]:0x61fcc0 value of arr[2][8]:0
ptr of arr[2][9]:0x61fcc4 value of arr[2][9]:0

ptr of arr[3]:0x61fcc8 value of test[3] itself:0x61fcc8 value of arr[3]:0 ptr of arr[3][0]:0x61fcc8 value of arr[3][0]:0
ptr of arr[3][1]:0x61fccc value of arr[3][1]:0
ptr of arr[3][2]:0x61fcd0 value of arr[3][2]:0
ptr of arr[3][3]:0x61fcd4 value of arr[3][3]:0
ptr of arr[3][4]:0x61fcd8 value of arr[3][4]:0
ptr of arr[3][5]:0x61fcdc value of arr[3][5]:0
ptr of arr[3][6]:0x61fce0 value of arr[3][6]:0
ptr of arr[3][7]:0x61fce4 value of arr[3][7]:0
ptr of arr[3][8]:0x61fce8 value of arr[3][8]:0
ptr of arr[3][9]:0x61fcec value of arr[3][9]:0

ptr of arr[4]:0x61fcf0 value of test[4] itself:0x61fcf0 value of arr[4]:0 ptr of arr[4][0]:0x61fcf0 value of arr[4][0]:0
ptr of arr[4][1]:0x61fcf4 value of arr[4][1]:0
ptr of arr[4][2]:0x61fcf8 value of arr[4][2]:0
ptr of arr[4][3]:0x61fcfc value of arr[4][3]:0
ptr of arr[4][4]:0x61fd00 value of arr[4][4]:0
ptr of arr[4][5]:0x61fd04 value of arr[4][5]:0
ptr of arr[4][6]:0x61fd08 value of arr[4][6]:0
ptr of arr[4][7]:0x61fd0c value of arr[4][7]:0
ptr of arr[4][8]:0x61fd10 value of arr[4][8]:0
ptr of arr[4][9]:0x61fd14 value of arr[4][9]:0

ptr of arr[5]:0x61fd18 value of test[5] itself:0x61fd18 value of arr[5]:0 ptr of arr[5][0]:0x61fd18 value of arr[5][0]:0
ptr of arr[5][1]:0x61fd1c value of arr[5][1]:0
ptr of arr[5][2]:0x61fd20 value of arr[5][2]:0
ptr of arr[5][3]:0x61fd24 value of arr[5][3]:0
ptr of arr[5][4]:0x61fd28 value of arr[5][4]:0
ptr of arr[5][5]:0x61fd2c value of arr[5][5]:0
ptr of arr[5][6]:0x61fd30 value of arr[5][6]:0
ptr of arr[5][7]:0x61fd34 value of arr[5][7]:0
ptr of arr[5][8]:0x61fd38 value of arr[5][8]:0
ptr of arr[5][9]:0x61fd3c value of arr[5][9]:0

ptr of arr[6]:0x61fd40 value of test[6] itself:0x61fd40 value of arr[6]:0 ptr of arr[6][0]:0x61fd40 value of arr[6][0]:0
ptr of arr[6][1]:0x61fd44 value of arr[6][1]:0
ptr of arr[6][2]:0x61fd48 value of arr[6][2]:0
ptr of arr[6][3]:0x61fd4c value of arr[6][3]:0
ptr of arr[6][4]:0x61fd50 value of arr[6][4]:0
ptr of arr[6][5]:0x61fd54 value of arr[6][5]:0
ptr of arr[6][6]:0x61fd58 value of arr[6][6]:0
ptr of arr[6][7]:0x61fd5c value of arr[6][7]:0
ptr of arr[6][8]:0x61fd60 value of arr[6][8]:0
ptr of arr[6][9]:0x61fd64 value of arr[6][9]:0

ptr of arr[7]:0x61fd68 value of test[7] itself:0x61fd68 value of arr[7]:0 ptr of arr[7][0]:0x61fd68 value of arr[7][0]:0
ptr of arr[7][1]:0x61fd6c value of arr[7][1]:0
ptr of arr[7][2]:0x61fd70 value of arr[7][2]:0
ptr of arr[7][3]:0x61fd74 value of arr[7][3]:0
ptr of arr[7][4]:0x61fd78 value of arr[7][4]:0
ptr of arr[7][5]:0x61fd7c value of arr[7][5]:0
ptr of arr[7][6]:0x61fd80 value of arr[7][6]:0
ptr of arr[7][7]:0x61fd84 value of arr[7][7]:0
ptr of arr[7][8]:0x61fd88 value of arr[7][8]:0
ptr of arr[7][9]:0x61fd8c value of arr[7][9]:0

ptr of arr[8]:0x61fd90 value of test[8] itself:0x61fd90 value of arr[8]:0 ptr of arr[8][0]:0x61fd90 value of arr[8][0]:0
ptr of arr[8][1]:0x61fd94 value of arr[8][1]:0
ptr of arr[8][2]:0x61fd98 value of arr[8][2]:0
ptr of arr[8][3]:0x61fd9c value of arr[8][3]:0
ptr of arr[8][4]:0x61fda0 value of arr[8][4]:0
ptr of arr[8][5]:0x61fda4 value of arr[8][5]:0
ptr of arr[8][6]:0x61fda8 value of arr[8][6]:0
ptr of arr[8][7]:0x61fdac value of arr[8][7]:0
ptr of arr[8][8]:0x61fdb0 value of arr[8][8]:0
ptr of arr[8][9]:0x61fdb4 value of arr[8][9]:0

ptr of arr[9]:0x61fdb8 value of test[9] itself:0x61fdb8 value of arr[9]:0 ptr of arr[9][0]:0x61fdb8 value of arr[9][0]:0
ptr of arr[9][1]:0x61fdbc value of arr[9][1]:0
ptr of arr[9][2]:0x61fdc0 value of arr[9][2]:0
ptr of arr[9][3]:0x61fdc4 value of arr[9][3]:0
ptr of arr[9][4]:0x61fdc8 value of arr[9][4]:0
ptr of arr[9][5]:0x61fdcc value of arr[9][5]:0
ptr of arr[9][6]:0x61fdd0 value of arr[9][6]:0
ptr of arr[9][7]:0x61fdd4 value of arr[9][7]:0
ptr of arr[9][8]:0x61fdd8 value of arr[9][8]:0
ptr of arr[9][9]:0x61fddc value of arr[9][9]:0

heap:
ptr of test[0]0x2593bb0
ptr of test[1]0x2593bb8
ptr of test[2]0x2593bc0
ptr of test[3]0x2593bc8
ptr of test[4]0x2593bd0
ptr of test[5]0x2593bd8
ptr of test[6]0x2593be0
ptr of test[7]0x2593be8
ptr of test[8]0x2593bf0
ptr of test[9]0x2593bf8
sizeof(test),(on a 64-bit compiler): 8
ptr of test[0]:0x2593bb0 value of test[0] itself:0x2593c40 value of test[0]:0 ptr of test[0][0]:0x2593c40 value of test[0][0]:0
ptr of test[0][1]:0x2593c44 value of test[0][1]:0
ptr of test[0][2]:0x2593c48 value of test[0][2]:0
ptr of test[0][3]:0x2593c4c value of test[0][3]:0
ptr of test[0][4]:0x2593c50 value of test[0][4]:0
ptr of test[0][5]:0x2593c54 value of test[0][5]:0
ptr of test[0][6]:0x2593c58 value of test[0][6]:0
ptr of test[0][7]:0x2593c5c value of test[0][7]:0
ptr of test[0][8]:0x2593c60 value of test[0][8]:0
ptr of test[0][9]:0x2593c64 value of test[0][9]:0

ptr of test[1]:0x2593bb8 value of test[1] itself:0x2593cb0 value of test[1]:0 ptr of test[1][0]:0x2593cb0 value of test[1][0]:0
ptr of test[1][1]:0x2593cb4 value of test[1][1]:0
ptr of test[1][2]:0x2593cb8 value of test[1][2]:0
ptr of test[1][3]:0x2593cbc value of test[1][3]:0
ptr of test[1][4]:0x2593cc0 value of test[1][4]:0
ptr of test[1][5]:0x2593cc4 value of test[1][5]:0
ptr of test[1][6]:0x2593cc8 value of test[1][6]:0
ptr of test[1][7]:0x2593ccc value of test[1][7]:0
ptr of test[1][8]:0x2593cd0 value of test[1][8]:0
ptr of test[1][9]:0x2593cd4 value of test[1][9]:0

ptr of test[2]:0x2593bc0 value of test[2] itself:0x2593d20 value of test[2]:0 ptr of test[2][0]:0x2593d20 value of test[2][0]:0
ptr of test[2][1]:0x2593d24 value of test[2][1]:0
ptr of test[2][2]:0x2593d28 value of test[2][2]:0
ptr of test[2][3]:0x2593d2c value of test[2][3]:0
ptr of test[2][4]:0x2593d30 value of test[2][4]:0
ptr of test[2][5]:0x2593d34 value of test[2][5]:0
ptr of test[2][6]:0x2593d38 value of test[2][6]:0
ptr of test[2][7]:0x2593d3c value of test[2][7]:0
ptr of test[2][8]:0x2593d40 value of test[2][8]:0
ptr of test[2][9]:0x2593d44 value of test[2][9]:0

ptr of test[3]:0x2593bc8 value of test[3] itself:0x2593d90 value of test[3]:0 ptr of test[3][0]:0x2593d90 value of test[3][0]:0
ptr of test[3][1]:0x2593d94 value of test[3][1]:0
ptr of test[3][2]:0x2593d98 value of test[3][2]:0
ptr of test[3][3]:0x2593d9c value of test[3][3]:0
ptr of test[3][4]:0x2593da0 value of test[3][4]:0
ptr of test[3][5]:0x2593da4 value of test[3][5]:0
ptr of test[3][6]:0x2593da8 value of test[3][6]:0
ptr of test[3][7]:0x2593dac value of test[3][7]:0
ptr of test[3][8]:0x2593db0 value of test[3][8]:0
ptr of test[3][9]:0x2593db4 value of test[3][9]:0

ptr of test[4]:0x2593bd0 value of test[4] itself:0x2593e00 value of test[4]:0 ptr of test[4][0]:0x2593e00 value of test[4][0]:0
ptr of test[4][1]:0x2593e04 value of test[4][1]:0
ptr of test[4][2]:0x2593e08 value of test[4][2]:0
ptr of test[4][3]:0x2593e0c value of test[4][3]:0
ptr of test[4][4]:0x2593e10 value of test[4][4]:0
ptr of test[4][5]:0x2593e14 value of test[4][5]:0
ptr of test[4][6]:0x2593e18 value of test[4][6]:0
ptr of test[4][7]:0x2593e1c value of test[4][7]:0
ptr of test[4][8]:0x2593e20 value of test[4][8]:0
ptr of test[4][9]:0x2593e24 value of test[4][9]:0

ptr of test[5]:0x2593bd8 value of test[5] itself:0x2593e70 value of test[5]:0 ptr of test[5][0]:0x2593e70 value of test[5][0]:0
ptr of test[5][1]:0x2593e74 value of test[5][1]:0
ptr of test[5][2]:0x2593e78 value of test[5][2]:0
ptr of test[5][3]:0x2593e7c value of test[5][3]:0
ptr of test[5][4]:0x2593e80 value of test[5][4]:0
ptr of test[5][5]:0x2593e84 value of test[5][5]:0
ptr of test[5][6]:0x2593e88 value of test[5][6]:0
ptr of test[5][7]:0x2593e8c value of test[5][7]:0
ptr of test[5][8]:0x2593e90 value of test[5][8]:0
ptr of test[5][9]:0x2593e94 value of test[5][9]:0

ptr of test[6]:0x2593be0 value of test[6] itself:0x2593ee0 value of test[6]:0 ptr of test[6][0]:0x2593ee0 value of test[6][0]:0
ptr of test[6][1]:0x2593ee4 value of test[6][1]:0
ptr of test[6][2]:0x2593ee8 value of test[6][2]:0
ptr of test[6][3]:0x2593eec value of test[6][3]:0
ptr of test[6][4]:0x2593ef0 value of test[6][4]:0
ptr of test[6][5]:0x2593ef4 value of test[6][5]:0
ptr of test[6][6]:0x2593ef8 value of test[6][6]:0
ptr of test[6][7]:0x2593efc value of test[6][7]:0
ptr of test[6][8]:0x2593f00 value of test[6][8]:0
ptr of test[6][9]:0x2593f04 value of test[6][9]:0

ptr of test[7]:0x2593be8 value of test[7] itself:0x2593f50 value of test[7]:0 ptr of test[7][0]:0x2593f50 value of test[7][0]:0
ptr of test[7][1]:0x2593f54 value of test[7][1]:0
ptr of test[7][2]:0x2593f58 value of test[7][2]:0
ptr of test[7][3]:0x2593f5c value of test[7][3]:0
ptr of test[7][4]:0x2593f60 value of test[7][4]:0
ptr of test[7][5]:0x2593f64 value of test[7][5]:0
ptr of test[7][6]:0x2593f68 value of test[7][6]:0
ptr of test[7][7]:0x2593f6c value of test[7][7]:0
ptr of test[7][8]:0x2593f70 value of test[7][8]:0
ptr of test[7][9]:0x2593f74 value of test[7][9]:0

ptr of test[8]:0x2593bf0 value of test[8] itself:0x7d6200 value of test[8]:0 ptr of test[8][0]:0x7d6200 value of test[8][0]:0
ptr of test[8][1]:0x7d6204 value of test[8][1]:0
ptr of test[8][2]:0x7d6208 value of test[8][2]:0
ptr of test[8][3]:0x7d620c value of test[8][3]:0
ptr of test[8][4]:0x7d6210 value of test[8][4]:0
ptr of test[8][5]:0x7d6214 value of test[8][5]:0
ptr of test[8][6]:0x7d6218 value of test[8][6]:0
ptr of test[8][7]:0x7d621c value of test[8][7]:0
ptr of test[8][8]:0x7d6220 value of test[8][8]:0
ptr of test[8][9]:0x7d6224 value of test[8][9]:0

ptr of test[9]:0x2593bf8 value of test[9] itself:0x7d6270 value of test[9]:0 ptr of test[9][0]:0x7d6270 value of test[9][0]:0
ptr of test[9][1]:0x7d6274 value of test[9][1]:0
ptr of test[9][2]:0x7d6278 value of test[9][2]:0
ptr of test[9][3]:0x7d627c value of test[9][3]:0
ptr of test[9][4]:0x7d6280 value of test[9][4]:0
ptr of test[9][5]:0x7d6284 value of test[9][5]:0
ptr of test[9][6]:0x7d6288 value of test[9][6]:0
ptr of test[9][7]:0x7d628c value of test[9][7]:0
ptr of test[9][8]:0x7d6290 value of test[9][8]:0
ptr of test[9][9]:0x7d6294 value of test[9][9]:0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章