在堆上创建的变量,指向同一变量的两个指针具有不同的地址?
我刚刚了解了堆栈和堆之间的区别。创建一个将为我在堆上动态分配内存的函数后,我返回指针并显示(在函数内和函数外)每个指针的地址和值。
这些值是相同的,这是我所期望的,但是堆上同一内存块的地址是不同的,这是我没想到的。
为什么? 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您报告的是指针的地址,而不是指针指向的地址。当然,
pTemp
和pHeap2
的指针地址会不同;这些是不同的指针,恰好指向内存中的同一地址。删除&
前缀pTemp
和pHeap2
以查看您期望的结果。图片是这样的:
这里,
&pTemp
是0042FBB0
,&pHeap2
是0042FCB4
。我为pTemp
和pHeap2
所指向的地址编写了一个地址(当然,结果可能因运行而异),如果您删除&
加上pTemp
和pHeap2
前缀,那么您会在每种情况下看到打印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
andpHeap2
; these are different pointers that happen to be pointing to the same address in memory. Remove the&
prefixingpTemp
andpHeap2
to see the results that you are expecting.The picture is something like this:
Here you have that
&pTemp
is0042FBB0
and&pHeap2
is0042FCB4
. I made up an address for the address thatpTemp
andpHeap2
are pointing to (of course, the results could vary from run to run anyway) and if you remove the&
prefixingpTemp
andpHeap2
then you would see0042FBC0
printed instead in each case.是的,
pTemp
和pHeap2
应该相同。但&pTemp
和&pHeap2
是不同的。这是因为&
运算符返回指向其操作数的指针。所以pTemp
是一个指向int
的指针,而&pTemp
是一个指向int
的指针。Yes,
pTemp
andpHeap2
should be the same. But&pTemp
and&pHeap2
are different. This is because the&
operator returns a pointer to it's operand. SopTemp
is a pointer to anint
, while&pTemp
is a pointer to a pointer to anint
.&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).