[英]why does output of an address of pointer is different?
i am confused concerning out of an program consider we have a class like below: 我对程序退出感到困惑,请考虑我们有一个如下所示的类:
#ifndef SOMECLASS
#define SOMECLASS
class SomeClass
{
public:
SomeClass();
SomeClass(int);
~SomeClass();
void foo(const int&);
}
#endif
and its implementation.... 及其实现...
so in main function: 所以在主要功能上:
int main(int argc, char **argv){
SomeClass* smc=new SomeClass();
cout<<smc<<"--"<<&smc<<"--"<<&*smc;
}
and my output a thing like below: 和我的输出如下所示:
0xf66210----0x7ffd622a34f0----0xf66210
why does it difference among smc and &smc and &*smc? 为什么smc和&smc和&* smc之间有区别? note that smc and &*smc are equal.
请注意,smc和&* smc相等。
i am using ubuntu(14.04_x64) and+cmake(2.18)+gcc(4.8.4) 我正在使用ubuntu(14.04_x64)和+ cmake(2.18)+ gcc(4.8.4)
smc
is the value of the pointer smc
, which is the address of what smc
is pointing to. smc
是指针smc
的值,它是smc
指向的地址。
&smc
is the address of the pointer smc
itself. &smc
是指针smc
本身的地址。
&*smc
is the address of what smc
is pointing to (the being pointed to is *smc
), so it is the same as smc
. &*smc
是smc
指向的地址(指向的是*smc
),因此它与smc
相同。
there are two variables here : 这里有两个变量:
1. the pointer, which is stack allocated 1.指针,它是堆栈分配的
2. the object, which is heap allocated 2.对象,这是堆分配的
smc
is the address of the actual object, which exists on the heap. smc
是存在于堆中的实际对象的地址。
consequently, &*smc
dereferences the address, then references it again, yealding the same result. 因此,
&*smc
取消引用该地址,然后再次引用该地址,获得相同的结果。 remember , *
and &
are like opposite operators, like plus and minus. 记住,
*
和&
就像是相反的运算符,例如加号和减号。 adding and subtracing the same amount yealds the same result, just like dereferencing and referencing again. 加上和减去相同的数量将获得相同的结果,就像取消引用和再次引用一样。
&smc
is the address of the pointer variable, which sits on the stack. &smc
是指针变量的地址,它位于堆栈上。
if it's still not clear to you, think about the following example: 如果您仍然不清楚,请考虑以下示例:
int* x = nullptr;
what is x
value? x
值是多少? and what is &x
? &x
什么?
and now? 现在?
x = new int(6)
what is the new value of x? x的新值是多少? and what is it's address?
那是什么地址?
To summarize the above, it can be said that: 综上所述,可以说:
smc
shows the address stored in the pointer (the address of the dynamically allocated ( heap ) memory using new
) smc
显示存储在指针中的地址(使用new
动态分配的( 堆 )内存的地址)
&smc
shows the address of the pointer itself &smc
显示指针本身的地址
*smc
shows the content of the address (access to the members of the object - of class SomeClass
) *smc
显示地址的内容(访问class SomeClass
的对象的成员)
&*smc
points to same address as smc
( "alias" of the pointer, ie same as smc
) &*smc
指向相同的地址smc
(指针的“别名”的,即相同的smc
)
A little bit more explanation. 多一点解释。 So basically
smc
is a variable right? 因此,基本上
smc
是变量吗? (a pointer, but a variable still). (一个指针,但仍然是一个变量)。 So
&smc
gives you the address of this variable . 因此
&smc
为您提供了此变量的地址 。
Now, if you try to print value of just smc
what should you get? 现在,如果您尝试仅打印
smc
值,您应该得到什么? value of the variable right? 变量的值正确吗? Since this is a pointer, in this case the value of this variable is address of another object to which it points.
由于这是一个指针,因此在这种情况下,此变量的值是它指向的另一个对象的地址。
Similarly &*smc
- dereferences the pointer and gives you back address of the object dereferenced, which is similar as above value. 同样,
&*smc
取消引用指针,并为您提供被取消引用对象的地址,该地址与上面的值相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.