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

Discussion Overview

The discussion centers around the concept of passing addresses in C/C++ programming, specifically how it affects variable manipulation within functions. Participants explore the mechanics of pointer usage and memory addressing in the context of a provided code example.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant presents a code example demonstrating how addresses of variables are passed to a function, allowing for their values to be swapped.
  • Another participant expresses confusion about the mathematical reasoning behind passing addresses, seeking clarification.
  • A later reply provides a simplified version of the code, explaining how memory locations are accessed and modified within the function.
  • Another participant agrees with the understanding but notes that the specific memory addresses mentioned are not representative of actual allocations, suggesting the use of a debugger for accurate addresses.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of passing addresses and the resulting behavior in the code. However, there is some disagreement regarding the representation of memory addresses and their allocation, with one participant correcting another's assumptions about specific memory locations.

Contextual Notes

There are limitations regarding the assumptions made about memory address allocation, as the actual addresses can vary based on the compiler and runtime environment. The discussion does not resolve these assumptions.

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