Does reference variable occupy memory? [duplicate] - c++

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.

Related

How can a pointer point to a reference variable? [duplicate]

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.

why const_cast is creating two values for one variable [duplicate]

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

Does sizeof(pointer) depend on the object type? [duplicate]

This question already has answers here:
Do all pointers have the same size in C++?
(10 answers)
The Definitive C++ Book Guide and List
(1 answer)
Is the sizeof(some pointer) always equal to four? [duplicate]
(17 answers)
Closed 8 years ago.
I'm trying to understand what a pointer to an object means. I'm wondering if A is an incomplete type, why is a pointer to A a complete type. Consider the following program:
#include <stdio.h>
class B; //B is incomplete type here
int main()
{
printf("%d\n",sizeof(B*));//4
}
Please explain why does sizeof(B*) return 4? What exactly does a pointer to an object represent in memory?
Please explain why sizeof(B*) return 4?
It returns 4 because size of a pointer variable is 4 in your system.
What exactly pointer to object represents in memory?
Objects reside in memory, and pointer to an object contains the starting memory address of that object. For example, if your B object has size 100 Byte, and it is placed in 1024-1123(100 Bytes) memory location, then a pointer to that object will hold the value `024 (starting address).
Is pointer to sizeof depends on the object type?
I guess you meant does pointer size depends on object type?. No, since pointers contain an address, it's size depends on address space of your system, not type of object it points to.
A pointer to an object represents the address of the object in memory. If in your case it is 4 bytes, it means that you have a 32 bit address space (either the OS, or your program is built in 32bits). To maintain the address of an object you don't really need any knowledge of the object, so a pointer to an incomplete type is fine.
Note that none of this applies to pointers to members, which are a completely different beast.

Strange behavior of const_cast [duplicate]

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

Modifying reference variables, not the variable they alias [duplicate]

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.