Call by address in C/C++, how does it work?

  • Topic: Comp Sci 
  • Thread starter shivajikobardan
  • Start date
  • Tags Tags
    Work
3 replies · 2K views · last reply
shivajikobardan
Messages
637
Reaction score
54
Homework Statement
How does changes in user defined function gets reflected in main function in call by address?
Relevant Equations
none
I've asked my question in the figure below as picture speaks 1000 words.
Handwritten notes on call by address: a void increment(int a) function with a++ and an int main() calling increment(x), then two boxes showing x at 2000H holding 20 and y at 4000H holding 30, and a change(&x, &y) call with void change(int *a, int *b).
Handwritten diagram of pointer memory: boxes labelled FFFFH holding 2000H and 2000H holding 20, and 0000H holding 4000H and 4000H holding 30, with arrows, then a = 2000H, b = 4000H, and the swap statements int k = *a, *a = *b, *b = k.

Handwritten notes on lined paper: values of *a = 30 and *b = 20, addresses of a = 2000H and b = 4000H (or alternatively &a = ffffH and &b = 0000H), and two questions asking why x and y are still 20 and 30 in main().

Handwritten notes on lined paper: "(8) Address of x = &x = 2000H" and "Address of y = &y = 3000H", followed by several lines of scribbled, partly illegible address notes.

Code:
Code:
#include<iostream>
using namespace std;
void change(int*,int*);
int main()
{
    int x,y;
    cout<<"Enter values  of x and y"<<endl;
    cin>>x>>y;
    change(&x,&y);
    cout<<"In main()"<<endl;
    cout<<"Values x="<<x<<" "<<"y="<<y<<endl;
    cout<<"Address x="<<&x<<" "<<"Address y="<<&y<<endl;
    return 0;
}
void change(int *a,int *b)
{
    int k=*a;
    *a=*b;
    *b=k;
    cout<<"In change()"<<endl;
    cout<<"Values x="<<*a<<" "<<"y="<<*b<<endl;
    cout<<"Address x="<<a<<" "<<"Address y="<<b<<endl;
    cout<<"Address x="<<&a<<" "<<"Address y="<<&b<<endl;
}

Output:

Enter values of x and y

20

30

In change()

Values x=30 y=20

Address x=0x79709ffdbc Address y=0x79709ffdb8

Address x=0x79709ffd90 Address y=0x79709ffd98

In main()

Values x=30 y=20

Address x=0x79709ffdbc Address y=0x79709ffdb8
 
the common reason i'm reading is "address gets passed so". but I'm failing to realize it mathematically like above.
 
I've got it.

C++:
int main( void ) {
    int x, y; // two variables, allocated on the stack, say at locations 0x4 and 0x8, values undefined
    change( &x, &y ); // receives 0x4 and 0x8
    return x + y; // accesses memory at 0x4 and 0x8, returning the sum of the values stored there
}

void change( int * a, int * b ) {
    *a = 3; // memory at location 0x4 accessed, value set to 3
    *b = 5; // memory at location 0x8 accessed, value set to 5
}
 
shivajikobardan said:
I've got it.
Yes, it seems like you do.
shivajikobardan said:
C++:
int main( void ) {
    int x, y; // two variables, allocated on the stack, say at locations 0x4 and 0x8, values undefined
    change( &x, &y ); // receives 0x4 and 0x8
    return x + y; // accesses memory at 0x4 and 0x8, returning the sum of the values stored there
}

void change( int * a, int * b ) {
    *a = 3; // memory at location 0x4 accessed, value set to 3
    *b = 5; // memory at location 0x8 accessed, value set to 5
}
Your understanding seems fine to me. The only comment I would make is that the compiler would never allocate stack variables at the locations you wrote. The actual locations would be very different. Although it's not crucial to know these addresses, you can see them if you use the debugger.