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

  • Comp Sci
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Work
In summary, the conversation discusses the use of pointers to pass addresses of variables as arguments in a function. The example provided shows how the values at those addresses can be changed by the function, and how they can be accessed in the main function. The use of a debugger can help visualize the actual addresses in memory.
  • #1
shivajikobardan
674
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.
_mImNe1sLJ3ywI1nnmZpeQd_G95jLMB_8DzGVx2WMpICpEMut8.png
JrEA4OoHgAGeJ35oAxi-DtO3_2oUUMY2qxMUAwvTPuSdLdvdTE.png

flLecFEQ9XrIJ8mJI7Y1Rrw1N2xR9FbnjUg8ILxa_dzM5ibLkQ.png

vK61Wnz1DlI0wwPU9sJJGdnalFg6-kAoetvKwyIR29OglRbuqQ.png

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
 
Physics news on Phys.org
  • #2
the common reason i'm reading is "address gets passed so". but I'm failing to realize it mathematically like above.
 
  • #3
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
}
 
  • #4
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.
 

1. What is call by address in C/C++?

Call by address in C/C++ is a method of passing arguments to a function by passing the address of the variable instead of the value itself. This allows the function to directly access and modify the value of the variable in the caller's scope.

2. How does call by address work?

In call by address, the address of the variable is passed as an argument to the function. The function then uses this address to access the value stored in that variable in the caller's scope. Any changes made to the value in the function will also be reflected in the caller's scope.

3. What is the difference between call by address and call by value?

In call by value, a copy of the variable's value is passed to the function, while in call by address, the address of the variable is passed. This means that call by value does not allow the function to modify the original variable in the caller's scope, while call by address does.

4. When should call by address be used?

Call by address is useful when we want to modify the value of a variable in the caller's scope from within a function. This can be used to save memory as passing the address is more efficient than passing the entire value. However, it should be used with caution as it can lead to unintended changes in the caller's scope.

5. Are there any drawbacks to using call by address?

One potential drawback of call by address is that it can make the code more difficult to read and understand, as it requires the use of pointers and dereferencing. Additionally, if the address passed to the function is invalid or the function does not handle it properly, it can lead to errors or unexpected behavior in the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
842
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
947
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top