Extending the lifetime of a temporary value in C ++

const int &ra=3;

      

  • As I know, creating a ra

    const will extend the lifetime of the temporary value r, which in this case is 3. This is a bit confusing as I know I ra

    must have the same address as the r-value, which is 3 here, but 3 is not a real variable and y it has no memory where it is stored. So how is this possible?

  • What's the difference between:

    const int& ra=a;
    
          

    and

    int& const ra=a;
    
          

+3


source to share


3 answers


but 3 is not a real variable and it has no memory where it is stored. so how is this possible?

Actually a temporary object is created from a literal 3

and then this temporary is associated with a constant reference. This is how it becomes possible.




Now your next question is the difference between these two

const int& ra=a;
int& const ra=a;

      

is that the second statement is illegal.

+12


source


1) Whether or not the compiler decides to actually keep your number 3

, the detail is up to the optimization decisions. As far as language is concerned, the temporary object lives as long as the reference. In practice, if you only want a value (not an object), the object can never be stored at all, and the compiler can instead substitute the value directly when you use ra

. Of course, if you take the address (via &ra

), then the compiler will make sure that the object is stored somewhere, so you can take its address. (It can still replace the value directly elsewhere, rather than load it from that address.)



2) The second version is not valid C ++. You can only say int const & x

and const int & x

for the same reason that int const

they const int

denote the same type. The link itself has no concept of persistence; it is always bound to the object it is initialized with (ie you cannot have a bare reference object ). int & x;

+8


source


From my point of view, when you create a const variable, it allocates memory where it is stored just like any other variable. Depending on the compiler, if you put this constant 3 in a function, it might emit a temp temperature of 3 every time you call this function, or it simply refers to a static value allocated when the program starts. This 3 is the real variable in that data that has been stored somewhere is like any other variable.

However, by referencing this value outside of its scope, it will behave like a crapshoot as to whether it will work, and therefore you shouldn't use it as a way to "extend the life" of a variable. You might be lucky to have a const allocated in a static location and always get the value you want, even when you access it from the scope; but you may be out of luck and end up grabbing the data written over the tempo you wanted. "Undefined" behavior means that compilers decide how they want to do it, so you can't count. Instead, increase the scope of the variable by declaring it earlier or by allocating it on the heap ( int* x = malloc(sizeof(int))//, etc.

and passing its pointer around.

0


source







All Articles