简体   繁体   English

为什么为传递给函数的指针获取不同的内存地址?

[英]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.

相关问题 为什么指针/变量内存地址不会改变? - Why do pointer / variable memory addresses not change? 变量,指针,对象和内存地址:为什么我会得到这个奇怪的结果? - Variables, Pointers, Objects and Memory Addresses: Why do I get this strange result? 为什么在赋值后会得到不同的指针值? - Why do I get different pointer values after an assignment? 为什么函数中的指针值与作为参数传递的指针值不同? - Why is the pointer value in the function different to the one passed in as an argument? 为什么同一指针有不同的地址 - Why does same pointer has different addresses 构造函数和成员函数中的指针返回不同的地址 - Pointer in constructor and member function return different addresses 当使用相似的逻辑递增两种不同的指针类型时,为什么会得到不同的地址? - Why am I getting different addresses when using similar logic to increment two different pointer types? function 指针地址在转换后是否保持不变? - Do function pointer addresses hold after conversions? 静态内存地址是否随其他计算机而变化? - Do static memory addresses change with different computers? 为什么我得到内存地址而不是实际值? 指针 c++ - Why Do I get the memory adress instead of the real value ? Pointer c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM