因此,這裏是我的代碼:爲什麼undereferenced指針具有不同勢的sizeof比提領 - C++
cout << "The size of an integer is " << sizeof(int) << endl;
cout << "The size of a double is " << sizeof(double) << endl;
cout << "The size of a string is " << sizeof(string) << endl;
int num = 1234;
cout << "The size of a num is " << sizeof(num) << endl;
int *pnum = #
cout << "The size of a pointer pnum is " << sizeof(pnum) << endl;
cout << "The size of a value pointed at by pnum is " << sizeof(*pnum) << endl;
return 0;
林通過此塊的代碼混淆,因爲這個輸出是:
The size of an integer is 4
The size of a double is 8
The size of a string is 40
The size of a num is 4
The size of a pointer pnum is 8
The size of a value pointed at by pnum is 4
我想的大小一個指針pnum應該和pnum指向的值相同,因爲我正在看一個教程「Programming C++」,在本教程中他們是一樣的。有人可以解釋爲什麼他們在我的程序中不一樣嗎?
由於基礎dataty pes有不同的尺寸? –
讓我猜,你在64位系統上?然後指針通常是64位,而「int」仍然是32位。 –
在教程中,它只是一個巧合,「int」與「int *」的大小相同。尺寸因系統而異。 – Galik