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.
 

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

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

Call by address, also known as call by reference using pointers, is a method of passing arguments to a function where the address of the variable is passed instead of the actual value. This allows the function to modify the original variable's value.

How do you implement call by address in C/C++?

To implement call by address, you need to pass the address of the variable to the function using the '&' operator, and the function should accept a pointer to the variable. Inside the function, you use the dereference operator '*' to access or modify the value at the address. For example:

void increment(int *num) {    (*num)++;}int main() {    int value = 5;    increment(&value);    // value is now 6    return 0;}

What are the advantages of using call by address?

Call by address allows functions to modify the original variables, which can be useful for operations that require changes to multiple variables. It also enables passing large data structures like arrays and structs efficiently, as only the address is passed rather than copying the entire structure.

What are the potential pitfalls of using call by address?

Using call by address can lead to bugs and undefined behavior if not handled carefully. Common issues include dereferencing null or invalid pointers, which can cause crashes, and unintended modifications to variables that can lead to difficult-to-debug errors. Proper validation and cautious handling of pointers are essential.

Can call by address be used with arrays?

Yes, call by address is particularly useful with arrays. When you pass an array to a function, you are actually passing the address of the first element of the array. This allows the function to modify the contents of the array. For example:

void modifyArray(int *arr, int size) {    for(int i = 0; i < size; i++) {        arr[i] *= 2;    }}int main() {    int numbers[] = {1, 2, 3, 4, 5};    modifyArray(numbers, 5);    // numbers array is now {2, 4, 6, 8, 10}    return 0;}

Similar threads

Replies
2
Views
3K
Replies
8
Views
1K
Replies
7
Views
1K
Replies
3
Views
1K
Replies
5
Views
2K
Replies
13
Views
2K
Replies
2
Views
1K
Replies
8
Views
2K
Back
Top