-3

Let me start by saying that I understand pointers. I know what they are and how they work. But I know them because I visually imagine how they are being used in memory by saving the address of something else and so on.

I'm trying to find information online and I can seem to find any information on how references are treated.

Could someone either link or explain how they work having memory management in mind?

thx

kebrus
  • 19
  • 6
  • That's actually compiler implementation specific. Compiler developers may be creative with available CPU instruction sets. – πάντα ῥεῖ Aug 23 '16 at 12:35
  • For a slightly different view, please also see [What is a reference variable in C++?](http://stackoverflow.com/questions/2765999/what-is-a-reference-variable-in-c) – Bo Persson Aug 23 '16 at 12:59

1 Answers1

-1

A reference is just a pointer with a thin layer of makeup.

Think of a reference as an ordinary pointer, except that every time you refer to a reference, the dereference operator, *, gets automatically used:

 int a;

 int &aref=a;

This is really the same as:

 int a;

 int *aref= &a;

Except that every time aref is referenced, the * operator gets applied, so

 void some_function_call(int);

 some_function_call(aref);

What's really happening here, with aref being a pointer:

 some_function_call(*aref);

If you were to take any C++ program, and replace every reference declaration with a pointer declaration (and an & address-of operator to initialize it), and then attach a * operator everywhere the reference, now a pointer, is used, the end result will be logically the same.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • _@Sam_ Sigh! We don't need to write an answer for each and every question :P – πάντα ῥεῖ Aug 23 '16 at 12:40
  • How would you rewrite `const int& x = 42;` to a pointer declaration? – cpplearner Aug 23 '16 at 12:42
  • Who would want to do something so silly? – Sam Varshavchik Aug 23 '16 at 12:42
  • @cpplearner `const int _tmp = 42; const int* x = &_tmp;` Since this is basically what happens. – Kevin Aug 23 '16 at 12:43
  • @Kevin Good, what about `[](const std::string&){}("something")`? – cpplearner Aug 23 '16 at 12:44
  • @cpplearner Same thing I guess. `{ const std::string _tmp("something"); [](const std::string*){}(&_tmp); }` In a block so the lifetime of the temporary is the same. – Kevin Aug 23 '16 at 12:46
  • @Kevin Now `auto size = [](const std::string& s){ return s.size(); }("something")`? – cpplearner Aug 23 '16 at 12:48
  • @cpplearner Almost the same with `std::string::size_type size; { const std::string _tmp("something"); size = [](const std::string* s){ return s->size(); }(&_tmp); }`. Replace `size` with a non-POD type and you got me :) – Kevin Aug 23 '16 at 12:52
  • Can someone actually reply to my question? I understand what references are, that was not my question, I just want to know what happens at low level. If this is a duplicate, please point me to it because I can't find that information anywhere. – kebrus Aug 23 '16 at 23:05
  • What part of "the same thing that happens 'at a low-level' (whatever that means) with a pointer, because a reference is just a pointer" you didn't understand? If you know what a pointer is and how it works, then you know all there is to know about references. – Sam Varshavchik Aug 23 '16 at 23:09
  • So does that means it'll save an address to the original object? – kebrus Aug 24 '16 at 02:14