正在打印什么? C++指向整数的指针
所以我有以下(非常简单)的代码:
int* pInt = new int(32);
std::cout<< pInt << std::endl; //statement A
std::cout<< *pInt << std::endl; //statement B
std::cout << &pInt << std::endl; //statement C
所以这就是我认为我正在做的事情(我了解到在 C++ 中我很少做我认为我正在做的事情):
- 创建一个指向一个整数并调用它 pInt
- 语句 A 打印值“32”的地址
- 语句 B 打印我的指针所指向的整数值(这样做是因为我取消引用该指针,从而使我可以访问它所指向的内容) 。
- statementsC 打印指针本身的地址(而不是整数值“32”的地址)。
这一切都是正确的吗?
So I have the following (very simple) code:
int* pInt = new int(32);
std::cout<< pInt << std::endl; //statement A
std::cout<< *pInt << std::endl; //statement B
std::cout << &pInt << std::endl; //statement C
So here's what I think I am doing (I have learned that in C++ I am rarely ever doing what I think I am doing):
- Creating a pointer to an integer and calling it pInt
- statementA prints the address of the value '32'
- statementB prints the integer value that is being pointed to by my pointer (which is done because I dereference the pointer, thus giving me access to what it is pointing to).
- statementC prints the address of the pointer itself (not the address of the integer value '32').
Is all of this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的第二个说法是错误的。您从堆中分配一个新的 int 。编译时常量“32”没有地址,因此您不能获取它。您创建了一个值为 32 的 int。这不是同一件事。
pInt1 和 pInt2 不相等。
哦,最后一件事 - C++ 是一种相当强类型的语言,几乎没有必要在变量名前添加类型数据前缀。这称为匈牙利表示法,它在 Microsoft C 库和示例中很常见,但在这里通常没有必要。
Your second statement is wrong. You allocate a new int off the heap. The compile-time constant "32" does not have an address, and therefore you cannot take it. You create an int whose value is 32. This is not the same thing.
pInt1 and pInt2 are not equal.
Oh, one last thing- C++ is a pretty strongly typed language, and it's pretty unnecessary to prefix variable names with type data. This is called Hungarian Notation, it's very common in Microsoft C libraries and samples, but generally unnecessary here.
std::cout<< pInt << std::endl; //语句A
std::cout<< *pInt << std::endl; //语句B,应该打印32
std::cout << &pInt << std::endl; //语句C
std::cout<< pInt << std::endl; //statement A
std::cout<< *pInt << std::endl; //statement B, which should print 32
std::cout << &pInt << std::endl; //statement C
语句 B 打印 pInt 指向的对象的值。它指向一个值为 32 的 int,因此它打印 32。
语句 A 和 C 未定义。在这两种情况下打印的对象是一个指针,在机器级别是一个内存地址。大多数编译器将打印十六进制数字,尽管不能保证它会这样做。该值还取决于对象的存储位置。例如,我的计算机上 32 位 Windows Vista 中的 g++ 打印:
Borland 打印:
Visual C++ 打印
您可能会得到不同的结果。第一个数字是 new int() 从堆中分配的地址,保存在 pInt 中。它分配了一个 4 字节数组,在那里存储数字 32,并将地址存储在 pInt 中。第二个数字是存储的值,解释为 int。第三个数字是 pInt 的地址(在堆栈上)。
Statement B prints the value of the object that pInt points to. It points to an int with the value 32, so it prints 32.
Statements A and C are undefined. The object being printed in both cases is a pointer, which at the machine level is a memory address. Most compilers will print a hexadecimal number, although there is no guarantee that it will do so. The value will also depend on where the objects are stored. For example, g++ in 32 bit Windows Vista on my computer prints:
Borland prints:
Visual C++ prints
You will probably get different results. The first number is the address allocated by new int() from the heap and saved in pInt. It allocated a 4 byte array, stored the number 32 there and stored the address in pInt. The second number is the value stored, interpreted as an int. The third number is the address (on the stack) of pInt.