#include <iostream>
using namespace std;
int main() {
int* z = new int(9);
cout << "address: " << z << endl;
cout << "value: " << *z << endl;
cout << "referance: " << &z << endl;
return 0;
}
Looking at the cout values, I was expecting the address and reference to give the same address, but heres what the output is:
address: 0x7fc452c032a0
value: 9
referance: 0x7fff5191b8d8
Just curious about the reason for this, is the plain value(z) the address of the variable in the heap with a value of 9, where var(&z) is address of the pointer variable which is located in the stack?
Here is a visualization:
Is the
&z designates the adress of the pointer int * z where you store the allocated adress new int(9).
The pointer z and the value 9 are stored at two different locations in memory.
There is not any notion of reference here, only adresses.
Let me go through some of the basics first.
A variable is a name that is used to refer to some location in the memory, a location that holds a value with which we are working.
Using '&' in C/C++ we can get the address of the variable.
A pointer is a variable that stores the address of a variable. For instance, in the example you are referring to
int* z = new int(9);
variable z stores the address of the value 9 [new int(9)].
Now, finally this variable has to be stored at some location in the memory and this can be accessed using ampersand (&).
&z //gives the address of the pointer to value 9 (address of variable z).
This is the same way the pointers and pointers to a pointer (multi level pointers) works.
int* z;
Above statement implies a pointer variable of int type declaration.
int* z = new int();
Above statement implies an address is allocated to pointer variable of int type dynamically.
int* z = new int(9);
Above statement implies value 9 is stored in a dynamically allocated.
cout << "address: " << z << endl;
Above line of code tells the address of pointer variable z.
cout << "value: " << *z << endl;
Above line of code tells the value stored in the variable z.
cout << "referance: " << &z << endl;
Above line of code tells the dynamically created variable's address.
Related
I am currently learning c++ and pointers inside of c++. I am curious as to why I get a different memory address when I reference a pointer and why it isn't the same as the reference to the variable. Here is my super simple code:
#include <iostream>
using namespace std;
int main() {
int n = 50;
int *p = &n;
cout << "Number: " << n << endl;
cout << "Number with &: " << &n << endl;
cout << "Pointer: " << p << endl;
cout << "Pointer with *: " << *p << endl;
cout << "Pointer with &: " << &p << endl;
}
Why does &p give me a different address than "&n" and "p"? Thank you
First you declare an int variable n with the value of 50. This value will be stored at an address in memory. When you print n you get the value that the variable n holds.
Then you declare a pointer *p pointing to the variable n. This is a new variable that will hold the address to the variable n, where it is stored in memory. When you print p you will get back that address.
You can also ask for the address to where the value of n is stored by using the & symbol. This is what you get when you print &n, this will be the same as the pointer p.
Using the * symbol before a pointer will dereference it, meaning instead of returning the address that is the pointer, it will return the value that the pointer is pointing to. In your case this is an int with the value 50.
Lastly, the pointer *p that you declared is stored at it's own address in memory. Just as you could get the address to the variable n with the & symbol, you can get the address of p by using &p. This will be different from the address of n since it's not where 50 is stored, it's where the pointer to the value 50 is stored.
#include <iostream>
using namespace std;
int main() {
int* z = new int(9);
cout << "address: " << z << endl;
cout << "value: " << *z << endl;
cout << "referance: " << &z << endl;
return 0;
}
Looking at the cout values, I was expecting the address and reference to give the same address, but heres what the output is:
address: 0x7fc452c032a0
value: 9
referance: 0x7fff5191b8d8
Just curious about the reason for this, is the plain value(z) the address of the variable in the heap with a value of 9, where var(&z) is address of the pointer variable which is located in the stack?
Here is a visualization:
Is the
&z designates the adress of the pointer int * z where you store the allocated adress new int(9).
The pointer z and the value 9 are stored at two different locations in memory.
There is not any notion of reference here, only adresses.
Let me go through some of the basics first.
A variable is a name that is used to refer to some location in the memory, a location that holds a value with which we are working.
Using '&' in C/C++ we can get the address of the variable.
A pointer is a variable that stores the address of a variable. For instance, in the example you are referring to
int* z = new int(9);
variable z stores the address of the value 9 [new int(9)].
Now, finally this variable has to be stored at some location in the memory and this can be accessed using ampersand (&).
&z //gives the address of the pointer to value 9 (address of variable z).
This is the same way the pointers and pointers to a pointer (multi level pointers) works.
int* z;
Above statement implies a pointer variable of int type declaration.
int* z = new int();
Above statement implies an address is allocated to pointer variable of int type dynamically.
int* z = new int(9);
Above statement implies value 9 is stored in a dynamically allocated.
cout << "address: " << z << endl;
Above line of code tells the address of pointer variable z.
cout << "value: " << *z << endl;
Above line of code tells the value stored in the variable z.
cout << "referance: " << &z << endl;
Above line of code tells the dynamically created variable's address.
I am a noobie in C++ and its pointers, and I don't understand why in this code it is showing different addresses. I am also having trouble to understand if pointers can hold values or only addresses.
int* y;
std::cout << "y address is: " << y << std::endl;
std::cout << "y address is: " << &y << std::endl;
Output:
y address is: 0x41c33e
y address is: 0x28fefc
The value of a pointer can be the address of a different object.
You get two different addresses printed because the pointer itself is also stored at some memory address:
int* y; // y is pointing to some address (where there is not yet a int!)
std::cout << "y address is: " << y << std::endl; // this is the adress
// the pointer is pointing to
std::cout << "y address is: " << &y << std::endl; // this is the memory
// address where the
// pointer is stored
What is a pointer?
Pointer is a variable that contains the address of another variable.
Here you take the address pointed to by the pointer:
std::cout << "y address is: " << y << std::endl;
As the pointer have his own address, here you take address of this pointer:
std::cout << "y address is: " << &y << std::endl;
That's why are two different addresses.
I think you are getting confused with what pointers actually are.
int y;
int* y_pointer = &y;
In the above, y holds an integer, and y_pointer holds the memory address that y resides in (note the & meaning address of). Pointers can only hold memory addresses (as they are pointers to memory elements), but this doesn't mean you can't access the variable they point to.
Dereferencing pointers using the * character can be used to do this.
There is a good question here about doing that for further reading.
//to understand it better lets do
int x;
int* y=&x;
The type of the variable y is int* which is an integer pointer.
y gives the memory address value that is stored in the integer pointer, which would be the memory location where x is stored.
&y is the address of the variable y itself, i.e the memory location where the pointer is stored.
I am new to cpp and learning pointers. while practicing the codes i came across this. When i do &(a pointer), it gives an another memory location.i want to know what this address is. I assume its the location where the ptr is saved. it has to be saved somewhere though it has the memory of value variable.
#include<iostream>
using namespace std;
int main()
{
int v =6;
int *ptr;
ptr = &v;
cout << "1: " << ptr << endl;
cout << "2: " << *ptr << endl;
cout << "3: " << &ptr << endl;
return 0;
}
OUTPUT:
1: 0x29cc6c
2: 6
3: 0x29cc68
Correct.
Output 1 is the value of the pointer, which is the address that it points to.
Output 2 is the "dereferenced" pointer which means it is the value contained at Output 1 (the location the pointer is pointing to).
Output 3 is the location where the pointer (which contains the value from output 1) is being stored.
You got it perfectly right:
v is a variable, so &v is the adress where v is stored in memory;
ptr is a variable as well, so &ptr is the adress where ptr is stored in memory.
In your example, because these variables are stored one after the other (on the local stack), their adresses are only 4 bytes apart.
I am new to C++ and I have a piece of code like this:
int firstvalue=10;
int * mypointer;
mypointer = &firstvalue;
cout << "pointer is " << *mypointer << '\n';
cout << "pointer is " << mypointer << '\n';
cout << "pointer is " << &mypointer << '\n';
The result is:
pointer is 10
pointer is 0x7ffff8073cb4
pointer is 0x7ffff8073cb8
Can anyone explain to me why the result of "mypointer" and "&mypointer" are different?
Thanks a lot.
mypointer is the value of the variable mypointer. And that value is, due to your assignment, the address of firstvalue.
&mypointer is the address of the variable mypointer. That is, the address of mypointer.
So, mypointer is the address of firstvalue, and &mypointer is the address of mypointer. Since firstvalue and mypointer are distinct variables, they have different addresses.
See inscribed comments
int firstvalue=10; // first variable, stored at say location 2000, so &firstvalue is 2000
int * mypointer; // second variable, stored at say location 2004, so &mypointer is 2004
mypointer = &firstvalue; // mypointer had garbage, now has 2000
cout << "pointer is " << *mypointer << '\n'; // contents of mypointer i.e. firstvalue (10)
cout << "pointer is " << mypointer << '\n'; // value of mypointer i.e. 2000
cout << "pointer is " << &mypointer << '\n'; // address of mypointer i.e. 2004
got it?
In the example, the & operator means "the address of". Therefore "mypointer" is the address of the value 10, but "&mypointer" is an address of an address whose value is 10.
firstvalue is a variable which can hold an int type value. This variable has its own address 0x7ffff8073cb4.
myvariable is a (pointer) variable that can hold the an int * type value, i.e address of a variable that can hold int type value. This variable has its own address 0x7ffff8073cb8.