infinite loop
infinite loop

Reputation: 1319

memory of a reference variable in c++?

I've just started to learn Cpp from the basics and am confused when I came across reference variables.

From what I have learn't reference variables are just like an alias (another name to the same memory), so in this case it need need any memory.

When I ran the below code:

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 the output as:

sample_class object size : 8

==>Here, the size for num is 4 bytes (32-bit compiler) and refnum since a reference is simply an alias to num. Then, why in this case, the size of object is 8?

==>Also, if really an refnum is like an alias then when does this information (info that refnum also holds/alias to the same memory address of num) gets stored?

Edited :

And consider this case (changine the definition of sample_class):

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

Here, If I print the object size of sample_class object, I get it as 12 (though the size of letter,refletter and letter_two are each equal to 1). But if I comment line 4, the object size is just 2. How is this happening???

I'm interested to learn from the basics, so if I'm wrong anywhere please correct me

Upvotes: 3

Views: 363

Answers (3)

Fran&#231;ois Andrieux
Fran&#231;ois Andrieux

Reputation: 29072

A reference is an alias, it should not be considered as a new variable. You cannot obtain its address and you cannot obtain its size. Any attempt to do so will instead obtain the address or size of the aliased object. In practice, most implementations implement them like pointers, but the standard does not require this. It makes no mention of the expected size for a reference.

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

References are not objects; they do not necessarily occupy storage, although the compiler may allocate storage if it is necessary to implement the desired semantics (e.g. a non-static data member of reference type usually increases the size of the class by the amount necessary to store a memory address).

Edit : The c++ standard gives a lot of leeway to implementations to decide for themselves the size of types and classes in order to accommodate the unique requirements of every architecture. In this case, padding is introduced between the members of your class. There is no requirement in c++ that a class's size must be equal to the sum of it's members' size. See Objects and alignment on cppreference.com for more information on the subject.

Edit 2 : There still seems to be some confusion regarding sizeof(T&).

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

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

The expression sizeof(T&) is treated as if you had written sizeof(T). This doesn't mean that the size of T& is equal to the size of T. It's simply that you cannot get the size of a reference directly with sizeof.

Upvotes: 8

nefas
nefas

Reputation: 1130

A reference store the address of the variable it reference (like pointer with stronger pre/post-conditions). This means that sizeof(T&) == sizeof(T*) == 4 on a 32bit architecture.

Comment from @FrançoisAndrieux about the real size of T&:

@nefas You state "This means that sizeof(T&) == sizeof(T*) == 4 on a 32bit architecture." but this is not true. Try it with a larger T. For example, sizeof(std::array<int, 10>&) is much larger than a pointer. You are taking the size of T, not T&.

An other thing that you have to take into account when calculating size of class/struct is the padding of the struct/class: the size of the struct maybe higher than the sum of the size of its member (I won't explain how the padding work because I haven't enough knowledge about it).

Upvotes: 1

Dr t
Dr t

Reputation: 247

In addition to the answer already provided I would recommend having a read of the material regarding padding at:

Data structure padding

Which is a good basic discussion regarding padding of classes and structures in C++ with simple examples to consider.

Upvotes: 2

Related Questions