Académique Documents
Professionnel Documents
Culture Documents
Computer Programming
Dr. Deepak B Phatak
Dr. Supratik Chakraborty
Department of Computer Science and Engineering
IIT Bombay
Session: Pointers in Function Calls
Why not?
Should it be call-by-value or call-by-reference?
Mostly call-by-value for our purposes
However, C++ allows passing references to pointers as well
References to pointer-valued (int *, char *, ) variables treated in
same way as references to variables of other basic data types (int,
char, )
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
{ int m; int n;
cout << Give m and n: ;
Activation
record of main
int main()
Address
Stack
Segment
0x740 0x743
m 0x005
0x780 0x783 0x006
n
{ int m; int n;
cout << Give m and n: ;
Activation
record of main
Activation
record of
swapByPtr
int main()
Address
Stack
Segment
0x740 0x743
m 0x005
0x780 0x783 0x006
n
0xa40 0xa43
ptrX 0x740
0xa80 0xa83 0x780
ptrY
0xab0 0xab3
temp
int temp;
temp = *ptrX;
*ptrX = *ptrY;
*ptrY = temp;
return;
Accessing contents of
memory location in
activation record of
main from swapByPtr
Activation
record of main
Activation
record of
swapByPtr
Address
Stack
Segment
0x740 0x743
m 0x005
0x780 0x783 0x006
n
0xa40 0xa43
ptrX 0x740
0xa80 0xa83 0x780
ptrY
0xab0 0xab3 0x005
temp
10
int temp;
temp = *ptrX;
*ptrX = *ptrY;
*ptrY = temp;
return;
}
Update contents of
memory at address
0x740
with
contents of memory
at address 0x780
Activation
record of main
Activation
record of
swapByPtr
Address
Stack
Segment
0x740 0x743
m 0x005
0x780 0x783 0x006
n
0xa40 0xa43
ptrX 0x740
0xa80 0xa83 0x780
ptrY
0xab0 0xab3 0x005
temp
11
int temp;
temp = *ptrX;
*ptrX = *ptrY;
*ptrY = temp;
return;
}
Update m in
activation record of
main with
value of n in
activation record of
main
Activation
record of main
Activation
record of
swapByPtr
Address
Stack
Segment
0x740 0x743
m 0x006
0x780 0x783 0x006
n
0xa40 0xa43
ptrX 0x740
0xa80 0xa83 0x780
ptrY
0xab0 0xab3 0x005
temp
12
int temp;
temp = *ptrX;
*ptrX = *ptrY;
*ptrY = temp;
return;
Accessing contents of
memory location in
activation record of
main from swapByPtr
Activation
record of main
Activation
record of
swapByPtr
Address
Stack
Segment
0x740 0x743
m 0x006
0x780 0x783 0x005
n
0xa40 0xa43
ptrX 0x740
0xa80 0xa83 0x780
ptrY
0xab0 0xab3 0x005
temp
13
int temp;
temp = *ptrX;
*ptrX = *ptrY;
*ptrY = temp;
return;
Contents of m and n in
activation record of
main are swapped !!!
Activation
record of main
Activation
record of
swapByPtr
Address
Stack
Segment
0x740 0x743
m 0x006
0x780 0x783 0x005
n
0xa40 0xa43
ptrX 0x740
0xa80 0xa83 0x780
ptrY
0xab0 0xab3 0x005
temp
14
int temp;
temp = *ptrX;
*ptrX = *ptrY;
*ptrY = temp;
return;
Contents of m and n in
activation record of
main are swapped !!!
Activation
record of main
Activation
record of
swapByPtr
Address
Stack
Segment
0x740 0x743
m 0x006
0x780 0x783 0x005
n
0xa40 0xa43
ptrX 0x740
0xa80 0xa83 0x780
ptrY
0xab0 0xab3 0x005
temp
15
{ int m; int n;
cout << Give m and n: ;
Activation
record of main
int main()
Address
Stack
Segment
0x740 0x743
m 0x006
0x780 0x783 0x005
n
Print m: 6
Print n: 5
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
16
{ int m; int n;
cout << Give m and n: ;
Activation
record of main
int main()
Address
Stack
Segment
0x740 0x743
m 0x006
0x780 0x783 0x005
n
Print m: 6
Print n: 5
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
17
18
}
19
}
20
void
swapByPtr(int
*ptrX,as
inthow
*ptrY);
Think
of swapByPtr
the void swapByPtr(int *ptrX, int *ptrY)
void swapByRef(int
&X, int &Y);
{ int temp;
compiler implements
temp = *ptrX; *ptrX = *ptrY;
int main() swapByRef
*ptrY = temp; return;
{ int m, int n;
Isnt
swapByRef
use?
cout <<
Give m, n:cleaner
; cin >> to
m >>
n; }
void swapByRef(int &X, int &Y)
swapByPtr(&m, &n);
{ int temp;
swapByRef(m, n);
temp = X; X = Y;
cout << m << << n << endl;
Y = temp; return;
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
}
22
}
23
Most certainly!
Care needed so that the returned pointer does not point to
a location in activation record of the function
Activation record freed when a function returns
Dereferencing an address in the freed activation record will cause
program to crash
24
25
26
27
28
29
30
Summary
IIT Bombay
31