Differences between passing by reference and passing by address

Deadly 提交于 2019-12-13 10:14:25

问题


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?


回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/52177329/differences-between-passing-by-reference-and-passing-by-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!