[英]Why Do I Get Different Memory Addresses For Pointer Passed To Function?
Basically my question is, when I run these two segments of code, I get different memory addresses. 基本上我的问题是,当我运行这两段代码时,我得到了不同的内存地址。 The first segment of code gives a certain memory address for rValue, and the second gives a different memory address for rValue, just by adding a & operator.
仅通过添加&运算符,第一段代码为rValue提供了一定的内存地址,第二段为rValue提供了不同的内存地址。 Why does this happen?
为什么会这样?
#include <iostream>
using namespace std;
int pMem(int *rValue){
cout << "Value of rValue is " << *rValue << endl;;
cout << "Address of rValue is " << rValue << endl;
*rValue = 15;
cout << "Value of rValue now is " << *rValue << endl;
cout << "Address of rValue is " << rValue << endl;
return *rValue;
}
int main() {
int value = 8;
int *pValue = &value;
pMem(&value);
cout << "Value = " << value << endl;
cout << "Address of Value: " << pValue << endl;
cout << "Value at memory address " << pValue << ": " << *pValue << endl;
return 0;
}
2nd block of code, this time with the &rValue... I get a different memory address than the first block of code. 第二个代码块,这次使用&rValue ...我得到的内存地址与第一个代码块不同。
#include <iostream>
using namespace std;
int pMem(int *rValue){
cout << "Value of rValue is " << *rValue << endl;;
cout << "Address of rValue is " << &rValue << endl;
*rValue = 15;
cout << "Value of rValue now is " << *rValue << endl;
cout << "Address of rValue is " << &rValue << endl;
return *rValue;
}
int main() {
int value = 8;
int *pValue = &value;
pMem(&value);
cout << "Value = " << value << endl;
cout << "Address of Value: " << pValue << endl;
cout << "Value at memory address " << pValue << ": " << *pValue << endl;
return 0;
} }
Even pointers themselves take up memory and have an address associated with them. 甚至指针本身也占用内存并具有与之关联的地址。
So &rValue is the address of the the pointer rValue, and, unless it's a pointer to a function, that address will be different. 因此&rValue是指针rValue的地址,并且,除非它是指向函数的指针,否则该地址将有所不同。
The reason for this behavior is that pointers are passed by value. 出现这种现象的原因是指针是按值传递的。 In other words, when your function receives a pointer-typed parameter
rValue
like this 换句话说,当您的函数收到像这样的指针类型的参数
rValue
int pMem(int *rValue)
C++ allocates space for a brand-new variable of type int*
, complete with its own address in memory. C ++为类型为
int*
的全新变量分配空间,并在内存中添加其自己的地址。 This variable gets initialized from a pointer expression that you pass to pMem
. 此变量从传递给
pMem
的指针表达式初始化。 Other than that, it is a separate variable that behaves like a local variable to pMem
. 除此之外,它是一个单独的变量,其行为类似于
pMem
的局部变量。 In particular, any re-assignments of the parameter itself have no effect on the caller. 特别是,参数本身的任何重新分配都不会对调用方产生影响。
It is the address of that parameter variable that gets printed when you do this: 执行此操作时,将打印出该参数变量的地址:
cout << &pValue << endl; // Prints a new address
If you would like to see the address that you passed to the function, print the pointer, not the address of it: 如果您想查看传递给该函数的地址,请打印指针,而不是其地址:
cout << pValue << endl; // Prints the address that you passed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.