lundi 30 octobre 2017

How c++ manage reference type variable? [duplicate]

This question already has an answer here:

Studying c++ these days. (I have C background). Recently I just found new feature of c++. It's called Reference. It's interesting syntactic sugar.

But I can't solve small curiosity. I can understand Reference is an alias for the variable. But I can't understand how it works internally.

For example.

    int data = 20;
    int &rData = data;
    cout << "Address in memory of data: " << &data << std::endl; // expected address of data
    cout << "Address in memory of rData: " << &rData << std::endl; // expected address of data

Address of data and address of rData is same. But how it works? data and rData has same value and same address. I searched stackoverflow a lot. But any of question treat this topic.(Maybe my poor searching skill makes me fail to search.)

So I assume that one hypothesis.

  • Maybe c++ internally have symbol table for the reference type, so reference type variable has no memory space in stack frame.

Is this hypothesis true? If not, how c++ treat this feature?

Aucun commentaire:

Enregistrer un commentaire