Question

two quesions: 1)how is reference variable stored in mem?

int a=1;
int &b=a;
cout<<&a<<endl<<&b;

the addr of a and b are same?so,b as a refrence variable takes no space in mem?!

2)refrence variable can only be initialized when defined,but if it can be initialized more than one time?how to wrote the code (just to tell the form itself only)?

int a=c=1;
int &b=a;
b=c;//i know this will change the source value of a,not re-assign the ref b,so
&b=c;//will this be ok?

No correct solution

OTHER TIPS

It is not specified by the language as to how references are stored, but most compilers will implement them as pointers internally.

Regardless, since references are "transparent" to you as a programmer, when you write &b you are in fact taking the address of the referand, not the reference. That's why you get the same address.

Similarly, writing &b=c does nothing to the reference itself, but writes the value c to pointer that is the address of a (a meaningless thing). It is the same as int* ptr = &b; ptr = c;

It is important to understand that & as "address of", and & as in reference type notation, are two different things:

int a;
int* ptr = &a; // <--- taking (and storing) the address of  `a`, i.e. a pointer

int& b = a;    // <--- declares a reference to `a`; `b` now behaves like `a`

// Two entirely different meanings of `&`.

There is also a third meaning of &, which is bitwise AND.

Anyway, if you want to re-seat a reference, you're out of luck. You simply cannot do that.

how is reference variable stored in mem?

As per § 8.3.2/4 of the Standard:

It is unspecified whether or not a reference requires storage

Therefore it's not guaranteed that references are stored in memory.


refrence variable can only be initialized when defined,but if it can be initialized more than one time?

Remember that & in:

int& x;

is part of the type. While & in:

&var

is the operator that will return the address of var.

They are two completely different things. One is used to declare a reference, the other (usually, because it can be overloaded) to take the address of some l-value.

  1. The fact that &b gives you the same value as &a does not say anything about where b is stored in memory (if at all). C++ language does not provide you with any means to determine the address of the reference itself. By definition, your &b evaluates to &a. It has absolutely no connection to the actual address of b's representation in memory, if such representation exists.

    Whether references have representation in memory or not is unspecified. In reality, it might easily vary from one context to another: some references will occupy memory, others will not. In your specific example it is very possible that the compiler will not create any memory representation for b. It will simply treat b as just another name for a.

    But even if compiler decides to allocate something in memory for b, you still won't be able to detect that fact by analyzing the value of &b. The value of &b is completely irrelevant here.

  2. As you said it yourself, "refrence variable can only be initialized when defined". There's no way to "redefine" an existing reference. There's no way to "reinitialize" it.

I think there is a confusion here between:

  • semantics: what the code you write mean and should do
  • implementation: how those semantics are translated into something the computer executes, and that produces the desired effects

The C++ Standard precises how references should behave (and notably, that you cannot have them reference another object or that taking their address should yield the same value as taking the address of the object they reference). This has nothing to do with how they are implemented under the hood... otherwise you would have no portability.

The C++ standard does not define how references are stored in memory. In fact, perhaps they don't always need to be stored at all, because the compiler may optimize them away. It depends on your compiler, your compiler options, your operating system and the context of the code within your program. It's completely impossible to give you a general answer for this case.

What is relevant is observable behaviour. And far as that is concerned, the reference is the object. It appears under a different name, but it is the same object. That's why as soon as you have the reference, taking its address is the same as taking the address of the object under its original name. Everything you do with the reference is the same as doing it with the object, including taking its address. Reference = object.

As you can see, this is very different from pointers. Pointers are objects of their own which may happen to point at other objects. You can take the address of the pointer itself, and it will be different from the address of the pointed-to object.

The fact that a reference to an object is the object also means that you cannot make a reference refer to something else later on. Remember, it's just a way to access one and the same object under a different name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top