正在打印什么? C++指向整数的指针

发布于 2024-11-07 01:31:54 字数 518 浏览 1 评论 0原文

所以我有以下(非常简单)的代码:

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++ 中我很少做我认为我正在做的事情):

  1. 创建一个指向一个整数并调用它 pInt
  2. 语句 A 打印值“32”的地址
  3. 语句 B 打印我的指针所指向的整数值(这样做是因为我取消引用该指针,从而使我可以访问它所指向的内容) 。
  4. 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):

  1. Creating a pointer to an integer and calling it pInt
  2. statementA prints the address of the value '32'
  3. 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).
  4. statementC prints the address of the pointer itself (not the address of the integer value '32').

Is all of this correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2024-11-14 01:31:54

你的第二个说法是错误的。您从堆中分配一个新的 int 。编译时常量“32”没有地址,因此您不能获取它。您创建了一个值为 32 的 int。这不是同一件事。

int* pInt1 = new int(32);
int* pInt2 = new int(32);

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.

int* pInt1 = new int(32);
int* pInt2 = new int(32);

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.

山人契 2024-11-14 01:31:54
  • 打印 pInt 的内存地址,它指向包含值 32 的内存区域(4 个字节):

std::cout<< pInt << std::endl; //语句A

  • 打印该内存地址的内容:

std::cout<< *pInt << std::endl; //语句B,应该打印32

  • 打印指针本身的内存地址:

std::cout << &pInt << std::endl; //语句C

  • Print the memory address of pInt, which points to a region of memory (4 bytes) that contains the value 32:

std::cout<< pInt << std::endl; //statement A

  • Print the content of that memory address:

std::cout<< *pInt << std::endl; //statement B, which should print 32

  • Print the memory address of the pointer itself:

std::cout << &pInt << std::endl; //statement C

壹場煙雨 2024-11-14 01:31:54

语句 B 打印 pInt 指向的对象的值。它指向一个值为 32 的 int,因此它打印 32。

语句 A 和 C 未定义。在这两种情况下打印的对象是一个指针,在机器级别是一个内存地址。大多数编译器将打印十六进制数字,尽管不能保证它会这样做。该值还取决于对象的存储位置。例如,我的计算机上 32 位 Windows Vista 中的 g++ 打印:

0x900ee8
32
0x22ff1c

Borland 打印:

016E33B0
32
0012FF50

Visual C++ 打印

00131D10
32
0028FE24

您可能会得到不同的结果。第一个数字是 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:

0x900ee8
32
0x22ff1c

Borland prints:

016E33B0
32
0012FF50

Visual C++ prints

00131D10
32
0028FE24

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文