Are references stored on the heap or stack?

后端 未结 4 637
被撕碎了的回忆
被撕碎了的回忆 2021-01-12 02:44

I wonder whether the reference variables such as c in this code:

int a = 5;
int & c = a;

are allocated from heap or stack.

相关标签:
4条回答
  • 2021-01-12 03:15

    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.

    0 讨论(0)
  • 2021-01-12 03:15

    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.

    0 讨论(0)
  • 2021-01-12 03:31

    Most compilers consider references as restricted pointers.

    0 讨论(0)
  • 2021-01-12 03:33

    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).

    0 讨论(0)
提交回复
热议问题