Reference variable memory in C ++?

I've just started learning Cpp from the basics and am confused when I come across reference variables.

From what I've learned, reference variables are like an alias (different name for the same memory), so it needs some memory in this case.

When I ran the code below:

class sample_class
{
    public:
        int num; //line1
        int& refnum = num; //line2
}

int main()
{
    sample_class sample_object;
    cout<< "sample_class object size : " << sizeof(sample_object) <<endl;
    return 0;
}

      

I got output as:

Sample_class object size: 8

==> Here the size for num

is 4 bytes (32-bit compiler) and refnum

since the link is just an alias num

. Then why, in this case, is the size of the object 8

?

==> Also, if it really refnum

looks like an alias, then when is this information (information that refnum

also contains a / alias for the same memory address num

) is stored?

Edited:

And consider this case (change definition sample_class

):

class sample_class
{
    public:
        char letter; //line3
        char& refletter = letter; //line4
        char letter_two; //line5
}

      

Here, if I print the size of the object of the object sample_class

, I get it as 12

(although the size is letter

, refletter

and letter_two

is 1

). But if I comment line 4

the object size is 2

. How does this happen?

I am curious to learn from the basics, so if I am wrong please correct me

+3


source to share


3 answers


A link is an alias and cannot be treated as a new variable. You cannot get your address and you cannot get your size. Any attempt to do this will instead get the address or size of the aliased object. In practice, most implementations implement them as pointers, but the standard does not require this. He does not mention the expected size for reference.

From: http://en.cppreference.com/w/cpp/language/reference

Links are not objects; they do not necessarily take up memory, although the compiler can allocate storage if it needs to implement the desired semantics (for example, a non-static data member of a reference type usually increases the size of the class by the amount needed to store the memory address).

Edit. The C ++ standard provides many opportunities for implementation to decide for yourself which types and classes will meet the unique requirements of each architecture. In this case, padding is introduced between the members of your class. In C ++, there is no requirement that the size of a class be equal to the sum of its size. See Objects and Alignment at cppreference.com for more information on this subject.



Edit 2: There seems to be some confusion about the relationship sizeof(T&)

.

From http://en.cppreference.com/w/cpp/language/sizeof :

When applied to a reference type, the result is the size of the reference type.

The expression is sizeof(T&)

processed as if you had written sizeof(T)

. This does not mean that size T&

is equal to size T

. It's just that you can't directly get the link size with sizeof

.

+9


source


In addition to the answer already provided, I would recommend reading the material about the add-on at:

Adding a data structure



This is a good basic discussion about populating C ++ classes and structures with simple examples.

+2


source


The link stores the address of the variable it refers to (for example, a pointer with stronger pre / post-conditions). This means that sizeof(T&) == sizeof(T*) == 4

on a 32-bit architecture
.

Comment from @ FrançoisAndrieux about real size T&

:

@nefas you point out "This means sizeof(T&) == sizeof(T*) == 4

for 32 bit architecture". But this is not the case. Try with a lot T

. sizeof(std::array<int, 10>&)

Much more than a pointer , for example . You are taking the size T

, not T&

.

Another value you should consider when calculating the size of a class / struct is struct / class padding: the size of a struct can be higher than the sum of the size of its member (I won't explain how shimming works because I don't have enough knowledge about it).

+1


source







All Articles