c++addressof

Does address-of operator return address of an object referenced by the variable


This is different question from this one, as here I'm using object type struct, instead of a value 23....

I'm reading this chapter on pointers and it states the following:

The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator. For example:

MyStruct* myvar = new MyStruct();
&myvar; // what is this address?

Is my understanding correct that it's the address of an object new MyStruct(), not the variable itself? I don't yet have good understanding of how variables (not objects they reference) are stored, and it's very likely that they are not used at all when program is compiled.


Solution

  • As per your previous question: No, it isn't... It is the address of variable myvar...

    EDIT

    so the actual bytes of new Struct() are stored under different address than a variable?

    new return address of memory where "bytes" of struct are allocated. Value of myvar will be the address where "bytes" are placed in memory, and &myvar is the address where variable myvar is placed in memory.

    -------------------------------------------------------
    |                     M E M O R Y                     |
    -------------------------------------------------------
    |   --------------                   ---------------  |
    |   | myvar = 234| ----points to---> | new MyStruct|  |
    |   --------------                   ---------------  |
    |   ^                                ^                |
    |   |                                |                |
    |   address 1 (&myvar)               address 234      |
    |                                                     |
    -------------------------------------------------------