-1
int func(int a, int& b){
   if (a < 3){
       return b;
   } else{
       b++;
       return func( a/10, b);
   }
}

I think b here is passed by pointer which is the same as passing by reference. What is passing by address, how it differs from passing by reference? Is there any variable in above is passed by address? Also, why func(40, 0) gave me an error as output?

OptatootatpO
  • 103
  • 2
  • 12
  • Ask only 1 question in question please. – user202729 Sep 05 '18 at 04:56
  • For the latter question: https://stackoverflow.com/questions/27463785/cant-pass-temporary-object-as-reference – user202729 Sep 05 '18 at 04:57
  • *passed by pointer which is the same as passing by reference* Can have similar results, but far from the same. – user4581301 Sep 05 '18 at 05:08
  • Possible duplicate of [Are there benefits of passing by pointer over passing by reference in C++?](https://stackoverflow.com/questions/334856/are-there-benefits-of-passing-by-pointer-over-passing-by-reference-in-c) – YesThatIsMyName Sep 05 '18 at 06:32
  • Passing a pointer means that there is potential for a null pointer (a pointer to a non-existent object) to be passed. Passing an argument by reference means the called function - and the compiler - can safely assume a valid object is passed - there is no way that the caller can pass a null reference without having introduced undefined behaviour (e.g. by dereferencing a null pointer). And a compiler is permitted to assume, at any point in time, that there has been no previous undefined behaviour. – Peter Sep 05 '18 at 08:37

2 Answers2

2

Let me try to make you understand in easy way. When you declared any variable in your c++ program then compiler create an entry in the symbol table for that variable and later an appropriate space in memory provided for it. In case of reference variable there will be a new entry in the symbol table which having the same storage of referenced varible, there will be no space allocated for it later, it is just an alias name like you may be refer by two names (like name, nick name). Now lets take a case of pointer varible. Irrespective of it is a pointer but it is a variable so it will also have a symbol table entry and space will be allocated for it later.

So from above statements you can easily find the below difference between address(pointer) and reference variable 1) There will no extra memory allocated for the reference variable but for pointer variable there will be 4 or 8 bytes depends on the system (32 or 64 bit operating system) for which you are going to compile and run the code. 2) you can't deference the reference variable later on normally so you can't changed the reference but in case of pointer variable it can contain different pointer.

The same is applicable for passing by reference and passing by address. Hope it will help you to understand in better way.

Try execute the below code and you will find that the address of variable and reference variable is same

int main()
{
  int i = 10;
  int& j = i;

  printf(" address of i = %u address of j = %u", &i, &j);
  return 0;
}
Sanjeev
  • 348
  • 2
  • 9
  • 1
    *"There will no extra memory allocated for the reference variable"* That is absolutely not guaranteed in any way. A compiler may even implement reference variables in the same way as pointers. – UnholySheep Sep 10 '18 at 08:16
  • @UnholySheep Mostly there should be no extra allocation, as mentioned there will be a new entry in the symbol table, generally compiler make a alias name for reference variable. Also number of reference variables you can use in the function call also restricted, I relate this also somehow to this. – Sanjeev Sep 10 '18 at 08:34
  • @UnholySheep To understand better adding the source code which you can run and will find that the variable and reference variable address are same. – Sanjeev Sep 10 '18 at 08:53
  • As has been answered [many times before](https://stackoverflow.com/questions/1950779/is-there-any-way-to-find-the-address-of-a-reference), references don't have their own address (as they aren't objects). They still (often) occupy space, your example code doesn't prove anything – UnholySheep Sep 10 '18 at 09:03
  • with respect please go through the below link https://www.cs.fsu.edu/~myers/c++/notes/references.html . As per my understanding if something occupy space in memory then it should contain the separate address as well, it shouldn't be pointing to the same address. – Sanjeev Sep 10 '18 at 09:23
  • The link you posted does not state that reference variables do not occupy memory and the answers in the one I posted directly addressed your misunderstanding. – UnholySheep Sep 10 '18 at 09:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179738/discussion-between-sanjeev-and-unholysheep). – Sanjeev Sep 10 '18 at 10:01
1

In practice, there ain't much difference between passing by reference or passing by pointer. Some complilers like MSVC model references exactly like that.

However when looking to the details it contains some surprising elements:

  • References look like regular variables, so no need for * or -> all over the place
  • References can't be nullptr, allowing slightly more performant code for static_cast (when you happen to achieve it, it is undefined behavior)
  • References don't have a fixed memory footprint, in practice it is the same a pointer, however, you can't rely on it
  • References can't be reassigned only the thing it points to can be changed
  • References ain't compatible with C

In general, you can look to references as special cases of pointers (or vise versa). Personally, I try to use references whenever possible and only fall back to pointers when required.

JVApen
  • 11,008
  • 5
  • 31
  • 67
  • *References can't be nullptr* Sadly references can still be null pointers. You just have to make multiple mistakes to pull it off. – user4581301 Sep 05 '18 at 05:14
  • 1
    @user4581301: if it becomes a nullptr it's undefined behavior. I know for a fact that MSVC allows it, while Clang optimizes/removes your code when depending on it – JVApen Sep 05 '18 at 05:29
  • To be fair to MSVC, it's not so much allowed as tolerated. But you see MSVC move towards improved standards conformance, so don't count on this. – MSalters Sep 05 '18 at 06:42
  • @MSalters I totally agree – JVApen Sep 05 '18 at 09:39