在堆上创建的变量,指向同一变量的两个指针具有不同的地址?

发布于 2024-10-05 07:27:53 字数 1680 浏览 5 评论 0原文

我刚刚了解了堆栈和堆之间的区别。创建一个将为我在堆上动态分配内存的函数后,我返回指针并显示(在函数内和函数外)每个指针的地址和值。

这些值是相同的,这是我所期望的,但是堆上同一内存块的地址是不同的,这是我没想到的。

为什么? pHeap2 和 pTemp 不应该指向同一个地址吗?

#include <iostream>
using namespace std;

int* intOnHeap(); // returns an int on the heap
int main()
{
 int* pHeap = new int; // new operator allocates memory on the heap and returns its address
       // 'new int' allocates enough memory on heap for one int and returns the address on the heap for that chunk of memory
       // 'int* pHeap' is a local pointer which points to the newly allocated chunk of memory
 *pHeap = 10;
 cout << "*pHeap: " << *pHeap << "\n\n";

 int* pHeap2 = intOnHeap();
 cout << "pHeap2:\n-----------" << endl;
 cout << "Address:\t" << &pHeap2 << "\n";
 cout << "Value:\t\t" << *pHeap2 << "\n\n";

 cout << "Freeing memory pointed to by pHeap.\n\n";
 delete pHeap;

 cout << "Freeing memory pointed to by pHeap2.\n\n";
 delete pHeap2;

// get rid of dangling pointers
    pHeap = 0;
    pHeap2 = 0;

 system("pause");
 return 0;
}

int* intOnHeap()
{
 int* pTemp = new int(20);
 cout << "pTemp:\n-----------" << endl;
 cout << "Address:\t" << &pTemp << "\n";
 cout << "Value:\t\t" << *pTemp << "\n\n";
 return pTemp;
}

输出:

*pHeap: 10

pTemp:
-----------
Address:        0042FBB0
Value:          20

pHeap2:
-----------
Address:        0042FCB4
Value:          20

Freeing memory pointed to by pHeap.

Freeing memory pointed to by pHeap2.

Press any key to continue . . .

I just learned the difference between the stack and the heap. After creating a function which will dynamically allocate memory on the heap for me, I return the pointer and display (in and out of the function) the address and value of each pointer.

The values are the same, which I expected, but the addresses to the same chunk of memory on the heap are different, which I did NOT expect.

Why? Shouldn't pHeap2 and pTemp point to the same address?

#include <iostream>
using namespace std;

int* intOnHeap(); // returns an int on the heap
int main()
{
 int* pHeap = new int; // new operator allocates memory on the heap and returns its address
       // 'new int' allocates enough memory on heap for one int and returns the address on the heap for that chunk of memory
       // 'int* pHeap' is a local pointer which points to the newly allocated chunk of memory
 *pHeap = 10;
 cout << "*pHeap: " << *pHeap << "\n\n";

 int* pHeap2 = intOnHeap();
 cout << "pHeap2:\n-----------" << endl;
 cout << "Address:\t" << &pHeap2 << "\n";
 cout << "Value:\t\t" << *pHeap2 << "\n\n";

 cout << "Freeing memory pointed to by pHeap.\n\n";
 delete pHeap;

 cout << "Freeing memory pointed to by pHeap2.\n\n";
 delete pHeap2;

// get rid of dangling pointers
    pHeap = 0;
    pHeap2 = 0;

 system("pause");
 return 0;
}

int* intOnHeap()
{
 int* pTemp = new int(20);
 cout << "pTemp:\n-----------" << endl;
 cout << "Address:\t" << &pTemp << "\n";
 cout << "Value:\t\t" << *pTemp << "\n\n";
 return pTemp;
}

Output:

*pHeap: 10

pTemp:
-----------
Address:        0042FBB0
Value:          20

pHeap2:
-----------
Address:        0042FCB4
Value:          20

Freeing memory pointed to by pHeap.

Freeing memory pointed to by pHeap2.

Press any key to continue . . .

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

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

发布评论

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

评论(3

怪异←思 2024-10-12 07:27:53

您报告的是指针的地址,而不是指针指向的地址。当然,pTemppHeap2 的指针地址会不同;这些是不同的指针,恰好指向内存中的同一地址。删除 & 前缀 pTemppHeap2 以查看您期望的结果。

图片是这样的:

0042FBB0           0042FBC0     0042FCB4
------------       ------       ------------
| 0042FBC0 |------>| 20 |<------| 0042FBC0 |
------------       ------       ------------
pTemp                           pHeap2

这里,&pTemp0042FBB0&pHeap20042FCB4 。我为 pTemppHeap2 所指向的地址编写了一个地址(当然,结果可能因运行而异),如果您删除 & 加上 pTemppHeap2 前缀,那么您会在每种情况下看到打印 0042FBC0

You are reporting the address of the pointers, not the address that the pointer is pointing to. Of course the address of the pointer will be different for pTemp and pHeap2; these are different pointers that happen to be pointing to the same address in memory. Remove the & prefixing pTemp and pHeap2 to see the results that you are expecting.

The picture is something like this:

0042FBB0           0042FBC0     0042FCB4
------------       ------       ------------
| 0042FBC0 |------>| 20 |<------| 0042FBC0 |
------------       ------       ------------
pTemp                           pHeap2

Here you have that &pTemp is 0042FBB0 and &pHeap2 is 0042FCB4. I made up an address for the address that pTemp and pHeap2 are pointing to (of course, the results could vary from run to run anyway) and if you remove the & prefixing pTemp and pHeap2 then you would see 0042FBC0 printed instead in each case.

陌生 2024-10-12 07:27:53

是的,pTemppHeap2 应该相同。但 &pTemp&pHeap2 是不同的。这是因为 & 运算符返回指向其操作数的指针。所以 pTemp 是一个指向 int 的指针,而 &pTemp 是一个指向 int 的指针。

Yes, pTemp and pHeap2 should be the same. But &pTemp and &pHeap2 are different. This is because the & operator returns a pointer to it's operand. So pTemp is a pointer to an int, while &pTemp is a pointer to a pointer to an int.

澜川若宁 2024-10-12 07:27:53

&pHeap2 不返回指针的值,它返回指针的地址。这是因为 &,在这种情况下意味着“地址”。

即在内存中的0042FCB4处,有一个指针指向0042FBB0(即在大端环境中,您会在0042FCB4处看到00 42 FB B0)。

&pHeap2 doesn't return the value of the pointer, it returns the address of the pointer. That's because of the &, which in this context means "the address of".

i.e. at 0042FCB4 in memory, there's a pointer pointing to 0042FBB0 (i.e. in a big-endian environment, you'd see 00 42 FB B0 at 0042FCB4).

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