-1

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.

Community
  • 1
  • 1
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

3 Answers3

4

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      |
|                                                     |
-------------------------------------------------------
Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61
  • thanks, so the actual bytes of `new Struct()` are stored under different address than a variable? – Max Koretskyi Apr 06 '17 at 07:01
  • `new` return address of memory where "mytes" of struct are allocated. So value of myvar will be the address where "bytes" are placed in memory, and &myvar is the address vere variable myvar is placed in memory. – LPs Apr 06 '17 at 07:04
  • yes, I know that, so suppose it returned `234`, and that is the address of the first byte of an object created by `new Struct()`, my question is essentially `&myvar` returns `234` or some other address? – Max Koretskyi Apr 06 '17 at 07:07
  • See my edit, I hope its clear enough. `234` is the value of `myvar`, the address will be different – LPs Apr 06 '17 at 07:09
  • thanks, it's clear now, so if `myvar` holds `address 2 `, how can I output it? – Max Koretskyi Apr 06 '17 at 07:19
  • I've edited my question details, so you can remove this `BTW your code is plain wrong: you mus...` – Max Koretskyi Apr 06 '17 at 07:20
  • Simply doing `cout << myvar << endl;` – LPs Apr 06 '17 at 07:25
  • just one more clarification, if I had `int myvar = 1232`, would I still get two addresses? Or would the address of the variable be actually the address of the first byte of the number `1232`? Can you please draw a picture like you did for `new MyStruct`? – Max Koretskyi Apr 06 '17 at 14:28
  • No It isn't. Address of `myvar` (i.e. `&myvar`) is the only address you can have. Value `1234` is placed in memory at address `&myvar` – LPs Apr 06 '17 at 14:30
  • thank you, so this assumption `Or would the address of the variable be actually the address of the first byte of the number 1232?` is correct? – Max Koretskyi Apr 06 '17 at 14:31
  • got it, thanks, that where my original confusion comes from. I'm wondering why it is different from the original case with `new`, any comments to read about? – Max Koretskyi Apr 06 '17 at 14:37
  • It is different because in that case at `myvar` address is stored the value of the address where the struct was allocated with new. So `myvar` always store a value, but the value has different "meanings" – LPs Apr 06 '17 at 14:42
2

Is my understanding correct that it's the address of an object new MyStruct(), not the variable itself?

No, it is the address of the variable myvar which was initialized with the object returned by new Struct();.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • thanks, so the actual bytes of `new Struct()` are stored under different address than a variable? – Max Koretskyi Apr 06 '17 at 07:01
  • For this code to compile, `myvar` has to be a pointer. So the question can't be answered, because it doesn't make any sense. – Lundin Apr 06 '17 at 07:01
  • @Maximus no, myvar point to the start of the address location, that's all. `&myvar` is where the `myvar` itself is located. – Sourav Ghosh Apr 06 '17 at 07:04
1

What you get is basically a copy of the pointer. Here's how you can prove it:

int* ptr = new int(2);
int& i = *ptr;
std::cout<<i<<std::endl; //prints 2
ptr = new int(3); //now ptr points to another address
std::cout<<*ptr<<std::endl; //prints 3
std::cout<<i<<std::endl; //still prints 2!

Ignoring bad memory management here, you see that you just have a reference, which is a copy to a memory location. If you change the original pointer, it doesn't change. Does that answer your question?

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189