简体   繁体   English

为什么指针地址的输出不同?

[英]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 . &*smcsmc指向的地址(指向的是*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.

相关问题 C ++:为什么我的函数返回的参考地址与解除引用的指针的地址不同? - C++: Why does my function return a reference address different from the dereferenced pointer's address? 为什么变量的地址与指针返回的地址不同? - why address of variable is different to what is returned by pointer? 为什么此指针包含存储在堆中的地址? - Why does this pointer contain an address stored on the heap? 为什么打印指针地址会触发断点? - Why does printing a pointer address trigger a breakpoint? 为什么指针的地址在传递时会发生变化? - Why does the address of pointer changes while passing? 为什么printf()在C ++指针中显示与cout不同的地址输出? - Why does printf() display a different address output than cout in C++ pointers? 为什么同一指针有不同的地址 - Why does same pointer has different addresses char * - 为什么指针中没有地址? - char* - why is there no address in the pointer? 如果堆栈在数字较低的地址处增长,为什么指针比较会反转这一点? - If the stack grows at numerically lower address why does pointer comparison reverses this? 为什么用指针调用GetWindowRect会导致异常,但地址却没有 - Why does calling GetWindowRect with pointer cause exception but address doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM