This question already has answers here:
Is there any way to find the address of a reference?
(10 answers)
Closed 2 years ago.
I am working with pointers and reference variable in C++.
int i=43;
int &refi=i;
What I know is the variable refi is bound to variable i. However it does not have its own memory. It is just a another name to the variable i. Then how can a pointer point to such unstored variable.
#include<iostream>
int main(){
int i=43;
int &refi=i;
int *p=&refi;
std::cout<<p;
return 0;
}
However I am not getting any error for the above code. Instead I am getting address of it.Am I wrong with the concept of reference variable here?If yes how?
The output is
0x61ff04
The reference has the same address in memory as the variable it's referencing. That essentially makes it an alias. Thus, when you take the address of a reference, you get the address of the original variable.
Related
This question already has answers here:
What does it mean to return a reference?
(4 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 3 months ago.
so I'm currently learning cpp (for fun at my own pace), specifically functions.
and i got a little confused, this is kinda beginner stuff
and it sometimes takes a little more time for me to understand..
i'm having a little hard time every time it comes to messing around with pointers and references
as things get more complicated the more i learn cpp.
i got this function, that confused me a bit:
int &get(int *arry, int index) { return arry[index]; }
int &get is a function of type int reference.
so i must return a reference.. (&)
does this means i'm returning a pointer ? but not dereferencing it ?
so the fact that i'm not dereferencing 'arry' means that i'm returning
the address of arry[index] ?
if so than address of the the reference operator are not the same
i may need a little order here.
thanks in advance.
This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 2 years ago.
First of all
I know the differences between & and * for variables but not for the objects of a class
consider I have a class let's call it a "Math"
Math math; // a simple object called math
Math* mathPtr = &math; // this will holds the address of math object
Math& mathRef = math ; // this one is exactly equivalent to the previous one
What are the differences ?!
A very big difference between pointers(*) and references(&) in nearly every situation is that a pointer is an indepedent variable and can be assigned NEW address values; whereas a reference, once assigned, can never refer to any new object until the variable goes out of scope.
Moreover a pointer can be of a null value, and a reference dosen't.
This question already has answers here:
Two different values at the same memory address
(7 answers)
Closed 5 years ago.
Consider the following code:
I declare a new reference end assign it to value a via const_cast. Then I just increase the reference value print the addresses and values.
#include <iostream>
using namespace std;
int main()
{
const int a = 7;
int &b = const_cast<int&>(a);
++b;
cout<<"Addresses "<<&a<<" "<<&b<<endl;
cout<<"Values "<<a<<" "<<b<<endl;
}
//output
Addresses 0x7fff11f8e30c 0x7fff11f8e30c
Values 7 8
How can i have 2 different values in the same address??
Because modifying a variable declared to be const is undefined behavior, literally anything can happen.
Modifying a constant object gives undefined behaviour, so your program could (in principle) do anything.
One reason for leaving this behaviour undefined is to allow the optimisation of replacing a constant variable with its value (since you've stated that the value can never change). That's what is happening here: a is replaced with the value 7 at compile time, and so will keep that value whatever you try to do to it at run time.
Even though const_cast may remove constness from any pointer or reference, using the resulting pointer or reference to write to an object that was declared const invokes undefined behavior.
check out the example here for more illustration:
http://en.cppreference.com/w/cpp/language/const_cast
Any attempt to modify an object that is itself declared const by means of const_cast results in undefined behavior according to the ISO C++ Standard.
When the we refer "const object", it intends to say that the memory where the object is located may be write-protected. That is, a variable or expression of const type may denote an object stored in write-protected memory and any attempt to modify the object results in undefined behavior
EDIT: Refer this site for more info
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1994/N0571.asc
This question already has answers here:
why reference size is always 4 bytes - c++
(2 answers)
Closed 7 years ago.
I have read that reference variable shares the same memory address with the original variable but also takes up some space on the stack.
And as reference has the same memory address as the original variable, it is also known as alias.
So, my question is how memory allocations are done for reference variables ?
8.3.2 References ยง4
It is unspecified whether or not a reference requires storage
That being said, if a reference needs storage, it typically needs as much storage as a pointer:
struct P
{
int* p;
};
struct R
{
int& r;
};
static_assert(sizeof(P) == sizeof(R), "sizeof(P) == sizeof(R)");
If the passed variable is located in memory, then a reference to that variable is usually a pointer, which could be kept in register or stored in memory. If the passed variable is located in a register due to compiler optimization, then a reference to that variable will use the same register.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ Reference, change the refered variable
I know that references in c++ are just pointers that get dereferenced for you when you use them. This question is about how to access the underlying pointer and change it.
Consider this code:
int x;
int& x_ref = x; //now equivalent to x
int* x_ptr = &x; //stores address of x
int* x_ref_ptr = &x_ref; //ALSO stores address of x
int&* x_ref_ptr_ref = ???; //what would this mean?
I'm trying to change where a reference points after initialization. I have no concern for type safety or proper practices. Does the c++ language have any tool to let me accomplish this?
There is no pointer to reference, it's ill-formed. A reference is an alias to an object. How would a pointer to an alias work?
Also, it's a feature of the language that a reference can't be reseated. A reseatable reference is a pointer.
This is not possible by design. By using a reference instead of a pointer, you decide to never change its target after declaration, with all entailing drawbacks and advantages (one of which is its "automatic dereferencing". Read the Wikipedia entry on references carefully.
You will need to switch to pointers.