次のコードを検討してください。アドレスを表すにはどれくらいのメモリが必要ですか?
int var;
cout << (long)&var;
私の疑問は、私たちがlong int型が&var
で示されるメモリ位置を保持するのに十分な幅を持っていることを知っている方法です。十分でない場合はどうすればよいですか?
私が実行しています完全なコード...
//: C03:YourPets2.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include <iostream>
using namespace std;
int dog, cat, bird, fish;
void f(int pet) {
cout << "pet id number: " << pet << endl;
}
int main() {
int i, j, k;
cout << "Address size " << sizeof(&f) << endl;
cout << "Long size " << sizeof(long) << endl;
cout << "Intptr size " << sizeof(intptr_t) << endl;
cout << "f(): " << &f << endl;
cout << "f(): " << (long)&f << endl;
cout << "f(): " << (long long)&f << endl;
cout << "dog: " << (long)&dog << endl;
cout << "cat: " << &cat << endl;
cout << "bird: " << &bird << endl;
cout << "fish: " << (long)&fish << endl;
cout << "i: " << (long)&i << endl;
cout << "i: " << (long long)&i << endl;
cout << "j: " << (long)&j << endl;
cout << "k: " << (long)&k << endl;
} ///:~
私は取得しています結果:
Address size 4
Long size 4
Intptr size 4
f(): 1
f(): 134514548
f(): 134514548
dog: 134521044
cat: 0x804a0d8
bird: 0x804a0dc
fish: 134521056
i: -1074729380
i: -1074729380
j: -1074729384
k: -1074729388
C++ 11では、ポインタ値を保持できることが保証されている ''の 'std :: intptr_t'を使用することができます。それはC99で初めて導入されました。 –