-1

return by reference function is done so that we would refer to the same variable so that we are not creating other variable for the function. so isn't it the the address of the returned variable and the function should be same.

int& add(int& one, int& two){
int c;
c=one+two;
cout<<"address of one is:"<<&c;
return c;

the main function

int main(){
  int a=10;
  int b=20;
 cout<<"address of add  function is: "<<(void*)&add<<endl;

both address are not equal.

  • 4
    the function and what it returns are two completely different things, how comes you think they should have the same address? – 463035818_is_not_an_ai Aug 22 '19 at 12:09
  • I don't think it is a duplicate of https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope. OP doesn't seem to understand the syntax of what he was inferring. – andresantacruz Aug 22 '19 at 12:13
  • 1
    if you allow a very far fetched analogy, this is similar to asking "Why is the value of + not 3?" – 463035818_is_not_an_ai Aug 22 '19 at 12:14
  • 1
    Not a duplicate (because it's not what you are asking) but read [this question (C++ Returning reference to local variable)](https://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable). Your `add` function is defective, you cannot return a reference to a local object from a function. – François Andrieux Aug 22 '19 at 12:22

2 Answers2

0

cout<<"address of add function is: "<<(void*)&add<<endl;

This line of code will not output the return of function add. The expression (void*)&add will actually return the address of the function add, not the return of it - that is it'll return the address of where the code of the function add is located in memory, not the address of the variable c.

Besides that typo, you probably got a warning by compiling this code as you are returning the reference of a local variable in the function add. Automatic storage duration variables like c should not consider to be allocated and valid anywhere outside of the scope where it is declared.

andresantacruz
  • 1,676
  • 10
  • 17
  • why did you use word virtual address not address of function on stack part of physical memory? – Parsuram Kailasa Aug 22 '19 at 13:17
  • @ParsuramKailasa because it is the correct term. The `&` operator returns the virtual address of its argument in your example not the address of physical memory. – andresantacruz Aug 22 '19 at 13:20
  • It is important to note though that this is not a terminology supported by the C++ standard as it is specific to the operating system. However this is how basically all modern operating system works in this intent. – andresantacruz Aug 22 '19 at 13:29
0

in main

cout<<"address of add  function is: "<<(void*)&add<<endl;

will show adress in memory of function add.

The function has its memory address. Not only output of your function, but the function itself.

(void*)&add returns address of function add.

Whats more, you dont even call function add in your main, so you can not compare addresses. You only check its address in virtual memory.

if you change it to

cout<<"address of add  function is: "<<(void*)&add(a,b)<<endl;

you will see that addresses match

but the address of function, and address of output of function can not be the same address. Those are two different things. Function can not return value to the address of function address. It would not make any sense.