I wonder whether the reference variables such as c
in this code:
int a = 5;
int & c = a;
are allocated from heap or stack.
References are just aliases and do not have to be allocated anywhere in particular. The standard doesn't specify this kind of detail, to the point where it isn't even required that references have any storage at all.
Doesn't matter, a reference is just a constant automatically de-referenced pointer without pointer arithmetic and without the option of a null or indeterminate value.
Regarding the re-formulated question - the way references are being used implies they will be mostly stack allocated. That is in all the non-static scenarios, where their storage cannot be omitted. And that's C++, a high performance programming language, that omits anything that is not being used.
Most compilers consider references as restricted pointers.
A reference is just an alias, and it is unspecified by the C++11 Standard whether it requires actual storage or not.
Per Paragraph 8.3.2/4 of the C++11 Standard:
It is unspecified whether or not a reference requires storage (3.7).