1

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? note that smc and &*smc are equal.

i am using ubuntu(14.04_x64) and+cmake(2.18)+gcc(4.8.4)

mehdi
  • 686
  • 2
  • 9
  • 21
  • 5
    Why would you expect the address of an object to be equal to the address of a pointer to this object? The object and the pointer to it are different objects, do they have different addresses. – Baum mit Augen Sep 06 '15 at 15:10

4 Answers4

7

smc is the value of the pointer smc, which is the address of what smc is pointing to.

&smc is the address of the pointer smc itself.

&*smc is the address of what smc is pointing to (the being pointed to is *smc), so it is the same as smc.

Allanqunzi
  • 3,230
  • 1
  • 26
  • 58
  • and is smc and *smc same? does *smc->foo(); and smc->foo() has any difference? – mehdi Sep 06 '15 at 16:07
  • 1
    `smc` and `*smc` are not the same, `smc` is a pointer, `*smc` is what the pointer is pointing to (an instance of `SomeClass`). `smc->foo()` means calling the member function `foo()` of that instance, `*smc->foo()` is illegal according to your code. It's better to read [a book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Allanqunzi Sep 06 '15 at 16:17
5

there are two variables here :
1. the pointer, which is stack allocated
2. the object, which is heap allocated

smc is the address of the actual object, which exists on the heap.
consequently, &*smc dereferences the address, then references it again, yealding the same result. 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.

if it's still not clear to you, think about the following example:

int* x = nullptr;

what is x value? and what is &x?

and now?
x = new int(6)
what is the new value of x? and what is it's address?

David Haim
  • 25,446
  • 3
  • 44
  • 78
  • ok, and is smc and *smc same? does *smc->foo(); and smc->foo() has any difference? – mehdi Sep 06 '15 at 16:09
  • 1
    +1 for the nullptr example. @mehdi They are not the same. In fact (*smc)->foo() will cause an error, since you are aplying operator -> to a non-pointer type. If you would have asked if (*smc).foo() and smc->foo() are the same, then yes, they are the same. The operator-> dereferences the pointer and accesses the member foo. This can be also achieved by dereferencing (using the *) and accesing the member via ".". – LoPiTaL Sep 06 '15 at 16:12
  • 1
    Last note about your question * smc->foo() is invalid here, since the operator-> takes precedence against operator *, so this statement would dereference smc implicitly, access and execute foo() member, and dereference its result. Thus, you have to use parenthesis: ( * smc)->foo() – LoPiTaL Sep 06 '15 at 16:16
1

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 shows the address of the pointer itself

  • *smc shows the content of the address (access to the members of the object - of class SomeClass)

  • &*smc points to same address as smc("alias" of the pointer, i.e. same as smc )

Ziezi
  • 6,375
  • 3
  • 39
  • 49
1

A little bit more explanation. So basically smc is a variable right? (a pointer, but a variable still). So &smc gives you the address of this variable.

Now, if you try to print value of just smc what should you get? 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.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • ok good explantion...but why does in c++ smc and *smc is same?! smc->foo(); and *smc->foo(); – mehdi Sep 06 '15 at 15:44
  • 1
    @mehdi:they are not the same. With printing function address might be different, I think that's not even possible, check here: http://stackoverflow.com/questions/11111969/how-to-print-member-function-address-in-c – Giorgi Moniava Sep 06 '15 at 15:49