Selected Topics in Computer Programming #2
Selected Topics in Computer Programming #2
Fine Points of C++ Pointers: Dumb, Smart, and Smarter
!
A little about me
Today’ Today’s topics
& & &
&
$ '( ) *+ , '. ( . . * / (( / 0 . # . ( 1 ( -. + % / 2 + 3( % , 3( (( // / . ( / ( / . . (( . & $ 1 ( # . 44"+ ( % 35. 6 77 . (( & . 77 6 . & / # % 0 . 8 ( # . ( / (( ( 9 (( # # :
.
"
#
$
#
; (
.
(( .
)
( . /
& 2
. ;
(
((
& ,
+
3 =
2
%
(
. (
(
3
' ( & 5#
( <
# ( 3
( .
3
*
#
(
& 2
((
#
3
Quick review of pointer nomenclature & 1
%
>
.
& $
%
#
% '# % 3#
%
<
% <
%
& $ %
#
/
0 .
%
#
% /
#
A pointer variable of pointer type > ?
% % ?*
/
%
#
%
#
( %
A pointer value of pointer type > ?
/>
(#
/
% $
3
)%
% '%
/
%
% 3
% @
*
%
(
& $
The pointerpointer-pointee relationship
>?
>?
$
4
.
& $ & $
.
# >?
/
A pointee of type >
# . (
> 5
% %
# (
$ %
'/ (
)
$
'/ (
%
%
. % * # )
A . % * 6
http://xkcd.com/c138.html
Other C++ pointerpointer-like entities &
/
. .
&
'
*
/
.
( (# ( (# &
% %
.
C
/
.
&
.
&
#D .
.
.
%
#D .
.
.( (# . & 8
9B
%
( (#
( (#
.
'
./
, & 8
.
*
( (#
.
/
.
7
C++ pointer use & E
A few more pointerpointer-related reminders '
/
? .
G F + HH I
G .
& J ? '. ( (
/
G "+ . HH I
?
*
.
$ &
8
KL
*
F '
(
/
HH
+
I HH
HH
+
% %
. (
8
% #
+ I HH
HH
%
#
.
*
77 M # (
& >
+
#
'
(
& $
HH I
..
'? * 0
& >
*
$ I HH
( (#
KL 0
/
(
%
%
# .
A
/
.
9
Some pointer behaviors are undefined &
/ (
&
/
%
? G + 0 G? +
/ 0
% . %
/
!
. (
% #
/ #
/
% H>L %' *+ > ? P# .<' *+
& $% # ) / % # 0
(
&
%$(
% %
/
- ..
/
# )
/
/
.
& $% *
. (
G F %N O+
( ..
# )
(
3 .=
3.
.
D
/ /
.
# / . (
* 3/
3
. /
(
A .
# ( (
'
# A ( (
'. /
' ((
11
.
/
.
/ ..
/
%
D 6
8 ( (
(
( %
( (
8 #
%
.
'( <
# )
- ..
&
%$(# %
& - (
/
!
.
& B (
Lifetime of C++ variables
6
? + ?=G +
10
. ( . /
*
A
( * 12
C++ dynamic lifetime management involves pointers
But using pointers is problematical [Koenig/Moo, 2000]
&
& TN$O
? .
P
'
* Q
!
!
"
#
$
(
8
%
(
' *+ R
& T
&
(
'* Q G .
?
P !
. ?
'
I HH ?
G S+ . HH I? + +
HH
& $
.
(
.
0 ( . '
+
T
. .< U
((
//
#
)
/
#
!
R
6
/D :*
( (
HH
U
.
(
+
)
I HH ?
. (
U %
(
!
%
((
!
(
HH I?
%
(
' " *+ &
% < % .
. %
% .
(
(
# .
13
Pointer issues 1: initialization & V
6 /.
% / .
%
6 /.
V
# % .
=
Pointer issues 2: copying
' /
/
:*
# /
%
#
.
/ &
& J/ .
% /
#
%
%
NAO. N
(
>? + . HH ? +
)
OU
<
G ' G
& $
.
//
.
/
O
G
#D . U +
$
!
,*,*,*
# (
%
>+
+ /'
*+
+
-/
%
*
/'>?
,* ,*,*
/ -.
<
'
+ + + + +
*+ +
( >?
(
>+
+ +
>' *
?
( 6
N
(
>? >? ?
%
/
. /
6 #
& T
#
& T /
14
%
* Q
+
+ R
15
Pointer issues 3: pointer destruction
Pointer issues 4: pointee destruction
& T
N ( (
O
& T
( < ( ( /' * Q > ? G >+ % '* Q % . H> ?L %+ % P# .< ' > *+ + R $
& J/ .
O . .
(
.
+ R
#
.
( ((
/N
> ?
$
G .
!
/
OU >+
HH ? +
+ + +
+ %+
*
>? /'* Q > +
(
/
.
%
( '
#
0
F +
( .
%
+
%
/
N
< U
$ %
//
16
.
R
6 * 17
18
Two common pointer misunderstandings &
.
http://xkcd.com/371
.
.
&
> >. >. >
?. ? ?. ?
.
(
+ + W+ +
=
(
>? G + / ' < G + < :G + G >+ + R + #
+ 77 < * Q
111
$*
9B
,
19
The moral of the story
The real problem is more general
& $
( .
.
.
&
(
/ (
( # (
<
.
>
% // .
#
. # (
. . .
# /
,
/ %
.
/ (
. ( .
3 0
> . # .
( '. ( ( ( #
#
A */ ( # (
(
(
<.
.
<=
. > / > /
/
- .<
3 .
0 (
(
(
1
((
..
&
#
<
>
( (
& , ( ( / (
( (
& 8
(
%
V
20
' ( .
%
'
(
. ((
* # .<
(
.*
. .
)
21
The resource management pattern & B
. (
(
8
.= . (
.
8
.
.
< .
. A
..
B
A
..
# /
/
,
3 0
.<
/ 3=
/
R . . ' 6
#
// .
.
. %
# * <
. > *+ > *+ > *+
. /
.= = .
(
.
Q >? ' >? ' >? W' .<
.
.=
. /
< '
& ,
0 ( 1
.
>
% !
%
%
(
. .=
&
& 1 /
. )
& J .
Issues in releasing resources
. . .
8 #
22
2 4 ! 4 !
.
. ! / 5
#
/ . 3
,
3 3
, ,
* Q , 6
,
,
R 23
24
Today’ Today’s topics .
Selected Topics in Computer Programming #2
#
; (
. )
Fine Points of C++ Pointers: Dumb, Smart, and Smarter
( . /
2
/
. 3 =
2
.
=
( . (
3
(
(
# ( 3
.
3
*
#
(
& 2
)* # (
(
( <
. (
(
' ( & 5#
)
(
((
& , ;
'
(( .
((
26
Resource management algorithms/policies & B
. (
(
Some realities of resource management
/.
(
& 8
. . 8
>
/
>
&
.
.
.
0 (
/
-
.
A
(
(
.
& 8
X
'* #
.
8
*
0.
#
.
.
.
(
78 8
(
.
'
.
3 # . %
A <
C*
%
( (
<
A
< /
.
(
(
. /
. rel="nofollow">
A
(
/
#
.
( U*
U*
(
. ( <
# /
. .
. .
/ /
'T8)
.
.< '.
( .
/
((
J
&
J-
>
#
J
. ( 'T
(
/
27
But resource management algorithms are separable &
.. >
(
.
8
/ .
((
%
77 . / ( . . . .
& 1
( . (
. (
(
. ( . /
(
/ (
/ ( '.
%
. ((
Characteristics of handles
<
.
28
&
.
.
H.
.
. < ?
.. /
(
. . *A
. /
29
/
.=
6 .
. ; (
A
P L
Y
#D . #D .
. . )
+
& B$88 ' 77
A
%
/ *
. (
#
# 0 /
& $ ( (
*
#
. . $
' 88
.
J ' < - %
.
.
( %
*
.
.
# A ) # .< 30
The RAII pattern & $
Recommended best practice
)
& $
) )
< (( (
J
( (# .
/
& B %
/ .
. =
.. #
%
( .
. A A
78 8 % .. % / (
( .
& >
0 (
H>L ' H>L ' H>L W '
2 0 0
!
3 3 3
/ 5
3
0 0
&
/
,
/ H>L
#
/
(
(
/
# .
VZ .) . . (%
.
.
+>(
#
'
;
< V *+
'
.
(
5 <
%
.
V
& $
> *+ > *+ > *+
%
.=
# . ( . .
0
3 #
(
.
*
<
.
#
H>L !
* !
$
31
32
But anonymous handles carry risks &
/' /
H>L'
.
.
/
&
= ,
/ (
,
/ ( #
& >
/. '*# /
.
.)
(
.
# W
#
9:
$
'*
> / +8 8 .
& $
*
* .
/ A // .
=
# H>L .
P
P
# # '
/ . %
J
? '
J
KL '
. /
A
# ( %
) (
)
.
# . ((
/' *
! '*
. ' P ) <
.
>
H>L' * /' *
H>L' *
L < (
/ (
/' *
H>L' * /' *
'*
'*
.
%
'* '*
H( (
.)
'*# /
'*
9
& . & Y & -
H>L .)
.
HL is today’ today’s only standard handle
P
> * ' * *+
.
*
( ( .<
%
%
. * /
A
.
..
( (# *
33
HL’ HL’s conversions can be problematic
P & >
)
(
78 8 .
.
>?
)
8
& 8(
P
.
%
. P P % /'
P P
/' *+
#
G
H>L
'
P
HL .
Other downsides to
%
&
>+
(
> *+
(
%
& 2
P
H L
'
N
O*+
$
/
NO
0
.
(
# . (
(
.
P
HL
HL .
. A ( ' 88
.
'**
= %
.
/
0
TJ
( % / ( %
J
.. .
35
P =
. (
/ .
.
.
(
( %
3 ( (#
. ) .
) %
*
HL
0 . . )
.
H L *+
HL
P
HL . /
& -
/
*
P
P >
>
# . Q R+ H L ' *+ H L # G + P
& , .
H>L .
H>L
34
.
< U
2 2
0A ( %
[ 36
More standard handles are on the way (C++0X) &
=
P
& J
, % # %
&
. ( ( % 0 . +
< <
P
/
P
2 %
%
78 8 # %
. ( %%
.
/
/
(
/ (
88
P HL/ ( . . .
%
.
(
0
( 88/ (
.
33# //
. ((
%
%
/
. (
(
(
.
#
P HL
%
/ ( . .
. (( V
.
/ /
%
,#
& -
HL
V
#
& 2 %
#
( ) % # ) (
(
.
.
6 % (
) %
.
HL
HL
V ( $
&
These new handles aren’ aren’t so new
HL
. ( ( # 0 (
.
/ %
#
. /
.
<
0
37
38
Passing handles to/from functions
The letterletter-envelope/pimpl pattern, part 1 ( 0 /
& >
& .
(
.
J J
&
( . .
#
(
.
)
# .
%
)
#D . P HL P HL
%
/
- #D . = P
( % HL
.
. '%
%! ;
'
/
( * . 0 * . +
'
.
.)
*
% .
!
. ' 77 M* ( %
( + P H (
+
<<=>
L P(
% ?
/
+ ?
+
R+ * / 39
The letterletter-envelope/pimpl pattern, part 2 ( 0 / & .
0/
(
# . ( ' / ' \( '* Q %
'. Q
/
. '# /
40
Advantages of the pimpl pattern & 8
* / '/ ( .P /. ' / *+ R
%
..) ..)
Q /
' * I I* * Q R 6P . *+ R
& ,
* .
$
& % Q
0/ P ( KL
' ( '/ ' 'F 0N O
/ (
(
# (
(
( 6
. (
&
#
% # . /.
(
.
.
%
(
%
0.
41
( .
% .
.
)
9( (# / .
. (
/ #
.
( .
. (
/ (
( * ** Q R 0 * . 0 6 ' * *+ R
/ . / (
(
; 0/ '
/
& 5(
R+ 0/ P(
/ ( (
. (
# 5 . 6
185 ? / + &
. $ Y (
(
? # / . /
)
!
0 / ' 0 / F* G / + 0/ F G' 0/ F*G \ 0/ '*G / +
/
. ' 8 8 ( % .) # ( % #
Q
(
.
.
> / 78 8 78 8
#
.
/
# %
&
/
0/ # . 0/ %
A .
/
; . >
&
/
# 0.
/
. .
/ 42
Other handlehandle-based programming patterns
Preventing clients from deleting pointees
& V
& .
P P
H%
>
&
H%
L
L %'
M *+
# . \M' * )
/ (
.
P &
./..
HL#
.
. .
.
A
+
P H H L
P
/ ( .
/
Q % \ '* Q R . + / . # .
/
#
.
& -
. % <
#
.
$
.
L . '
P '* Q
!
' * *+
+ /
.
3/
.
(
R
#D . /
R+
) .)
&
. %
Q '*'
? * Q
+ R
R+ 43
Pointing to a staticstatic-lifetime object &
. %
P
P '*'%
44
Executing cleanup code on block exit
Q
& 78 8
.
?* .
P H>L . . > ' *+ P
P '* Q
- . ( .
!
# .
.
P
(
%
# .<
. /
(
0. %
#
A
.
& Q P
%
( .
V H>L ' F P
/' #*
%
R+ &
.
Q R
P H%
HL%
L
#
'
'*
*+
.
(
.P. H % ? L' * # '/ #*
*+
R R
& - (
#
' 88 0 .
/ -
*
.
%
#
.
P
HL%
(
45
46
Transactional (commit(commit-oror-rollback) semantics, part 1
Transactional (commit(commit-oror-rollback) semantics, part 2
&
%$& V
0 .
Y . $ 0
< .
.
.
-. , V@
# .< 0
Y
/ $.. (
@
! A
7)
-.
(@ !
. (#
& 0 .
0 .
&
Y # .< '
. /
#
#
.
.
#
#
& $
)
/
185 ? / G -. Y %
.
# .< 0 . / .
/. # .<
' I( / 0 I I I *+ G , < Y ' /. '/*
% 3
%
/ *+
8/
.
47
/
.. / # .< ..
. ( . *.
/
)
.
. /
.
/ ( # .<
#
. 8/
( .
.
%
( . D
/ ) )
# .<
( ' # . ((
* . '. ( # #
% .
* / .
48
Dealing with legacy code & -. .
In sum
.
( // . #
(
& -
(
(
#
& -(
(
..
& -(
# = ' '
(( #
*+
# %
$
.
/
% & -(
%
]
(
# <
/
]
(
# <
; %
<
(
. ' % . )
*
(( . . (%
<
((
%
< =
< <
#
% 49
18; Selected Topics in Computer Programming #2
Fine Points of C++ Pointers: Dumb, Smart, and Smarter
!
.
/<
3( /
(
& -(
-.% -/%
(
/ .
(( .
P H>L ' ' * *+ ' * *+
&
(( (
"
#
$
%
50