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

  • Context: Comp Sci 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Work
Click For Summary
SUMMARY

The discussion focuses on the concept of "call by address" in C/C++, specifically how pointers are used to manipulate variable values directly in memory. The provided code demonstrates passing the addresses of two integer variables, x and y, to a function named change, which swaps their values. The output confirms that the values of x and y are successfully exchanged, illustrating the mechanics of pointer dereferencing and memory address manipulation. Participants emphasize the importance of understanding memory allocation and the role of the stack in variable storage.

PREREQUISITES
  • Understanding of C/C++ syntax and structure
  • Knowledge of pointers and memory addresses
  • Familiarity with stack memory allocation
  • Basic debugging skills to inspect memory addresses
NEXT STEPS
  • Study C/C++ pointer arithmetic and memory management
  • Learn about stack vs. heap memory allocation in C/C++
  • Explore debugging tools to visualize memory addresses and variable states
  • Investigate advanced topics like dynamic memory allocation with malloc and free
USEFUL FOR

C/C++ developers, computer science students, and anyone interested in understanding low-level memory manipulation and function parameter passing techniques.

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.
_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
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K