V
venkatagmail
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1[]) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed array a, which is &a
in the main method. However, the addresses of a[0] and a[1] in the
caller or main function are same as those of the array a1[0] and a1[1]
in the called function fun1(int a1[]). Any modifications to the array
in this function will reflect in the main method. How and where are
these variables created in stack, heap, tables and what happens after
and before calling the function?
Lets look at the other function fun2(int *&a2) where I am trying to
again pass by reference, but this time I do not want to create another
pointer but directly work on the original pointer pointing to the
array. When I pass this array a to this function it returns the
following error which I did not understand
fun2(a); // ERROR: invalid initialization of non-const reference of
//type 'int*&' from a temporary of type 'int*'
however, if I create another pointer int *aPtr = a; and then pass it
as fun2(aPtr); it works fine. Also in this case, I am working directly
with aPtr in both caller(main) and called (fun2) methods. i.e., &aPtr
is same in the caller and called functions, &a2 in fun2(int* &a2),
unlike the previous case, even this is pass by reference. I did not
have time to figure out the problem, so I am posting it here for
discussion and would like to know the details, as to how these
variables are created or assigned in memory.
#include<iostream>
using namespace std;
void fun1(int a1[]) // same as fun(int* p)
{
cout << "In fun(int a1[]) " << "a1 " << a1 << " &a1 " << &a1 << "
&a1[0] " << &a1[0] << endl;
// int a1[] is in a different location (different &a) but has the
// correct addresses of a1[0] and a1[1]
a1[0] = 10;
a1[1] = 20;
}
void fun2(int* &a2)
{
cout << "In fun1(int* &a2) " << " a2 " << a2 << " &a2 " << &a2 << "
&a2[0] " << &a2[0] << endl;
a2[0] = 30;
a2[1] = 50;
}
int main()
{
int a[2] = {0,1};
cout << "Main: a " << a << " &a " << &a << " &a[0] " << &a[0] <<
endl;
//fun2(a); // ERROR: invalid initialization of non-const
reference of
//type 'int*&' from a temporary of type 'int*'
fun1(a);
cout << "After fun(a): " << a[0] << ' ' << a[1] << endl;
int *aPtr = a;
cout << "Passing aPtr whose &aPtr " << &aPtr << " aPtr " << aPtr
<< endl;
fun2(aPtr); // Here it is ok, you can also use it as fun1(aPtr)
cout << "After fun1(aPtr): " << a[0] << ' ' << a[1] << endl;
int cInt = 10;
//int* &cPtr = &cInt; // WRONG:logically wrong as you are trying
to create a
// non-const reference of type int* & from
a
// temporary of type int*
int* cPtr = &cInt; // store the address of cInt in cPtr
int* &cRefPtr = cPtr; // make a reference of the cPtr
}
output:
Main: a 0x22ff80 &a 0x22ff80 &a[0] 0x22ff80
In fun(int a1[]) a1 0x22ff80 &a1 0x22feb0 &a1[0] 0x22ff80
After fun(a): 10 20
Passing aPtr whose &aPtr 0x22ff7c aPtr 0x22ff80
In fun1(int* &a2) a2 0x22ff80 &a2 0x22ff7c &a2[0] 0x22ff80
After fun1(aPtr): 30 50
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1[]) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed array a, which is &a
in the main method. However, the addresses of a[0] and a[1] in the
caller or main function are same as those of the array a1[0] and a1[1]
in the called function fun1(int a1[]). Any modifications to the array
in this function will reflect in the main method. How and where are
these variables created in stack, heap, tables and what happens after
and before calling the function?
Lets look at the other function fun2(int *&a2) where I am trying to
again pass by reference, but this time I do not want to create another
pointer but directly work on the original pointer pointing to the
array. When I pass this array a to this function it returns the
following error which I did not understand
fun2(a); // ERROR: invalid initialization of non-const reference of
//type 'int*&' from a temporary of type 'int*'
however, if I create another pointer int *aPtr = a; and then pass it
as fun2(aPtr); it works fine. Also in this case, I am working directly
with aPtr in both caller(main) and called (fun2) methods. i.e., &aPtr
is same in the caller and called functions, &a2 in fun2(int* &a2),
unlike the previous case, even this is pass by reference. I did not
have time to figure out the problem, so I am posting it here for
discussion and would like to know the details, as to how these
variables are created or assigned in memory.
#include<iostream>
using namespace std;
void fun1(int a1[]) // same as fun(int* p)
{
cout << "In fun(int a1[]) " << "a1 " << a1 << " &a1 " << &a1 << "
&a1[0] " << &a1[0] << endl;
// int a1[] is in a different location (different &a) but has the
// correct addresses of a1[0] and a1[1]
a1[0] = 10;
a1[1] = 20;
}
void fun2(int* &a2)
{
cout << "In fun1(int* &a2) " << " a2 " << a2 << " &a2 " << &a2 << "
&a2[0] " << &a2[0] << endl;
a2[0] = 30;
a2[1] = 50;
}
int main()
{
int a[2] = {0,1};
cout << "Main: a " << a << " &a " << &a << " &a[0] " << &a[0] <<
endl;
//fun2(a); // ERROR: invalid initialization of non-const
reference of
//type 'int*&' from a temporary of type 'int*'
fun1(a);
cout << "After fun(a): " << a[0] << ' ' << a[1] << endl;
int *aPtr = a;
cout << "Passing aPtr whose &aPtr " << &aPtr << " aPtr " << aPtr
<< endl;
fun2(aPtr); // Here it is ok, you can also use it as fun1(aPtr)
cout << "After fun1(aPtr): " << a[0] << ' ' << a[1] << endl;
int cInt = 10;
//int* &cPtr = &cInt; // WRONG:logically wrong as you are trying
to create a
// non-const reference of type int* & from
a
// temporary of type int*
int* cPtr = &cInt; // store the address of cInt in cPtr
int* &cRefPtr = cPtr; // make a reference of the cPtr
}
output:
Main: a 0x22ff80 &a 0x22ff80 &a[0] 0x22ff80
In fun(int a1[]) a1 0x22ff80 &a1 0x22feb0 &a1[0] 0x22ff80
After fun(a): 10 20
Passing aPtr whose &aPtr 0x22ff7c aPtr 0x22ff80
In fun1(int* &a2) a2 0x22ff80 &a2 0x22ff7c &a2[0] 0x22ff80
After fun1(aPtr): 30 50