Sie sind auf Seite 1von 9

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( % & 2 (( <
, 3( (( // / . ( + & , . . ( (
/ ( / . . (( .
; 3 = ( 3 # ( 3 . 3
& $ 1 ( # . 44"+ ( % 35-
. 6 77 . (( 2 ' ( *
& . 77 6 . & 5# # (
& / # % 0 . & 2 ((
8 ( # . ( /
(( ( 9 #
(( # # :

3 4

Quick review of pointer nomenclature The pointer-


pointer-pointee relationship
& 1 % > >? A pointer value
of pointer type > ?
. >? % '# * % />
& $ % # % % #
$ ( 3 % 3# 3 (#
& $ % % / % #
A pointer variable
@ < % # )% % % A pointee of type >
of pointer type > ?
< % / % . # . (
% '% 0 . / ?* & $ % #
& $ % # % . & $ % (
% / ( % # $ % '/ ( ) . % * A
$ % # / >? / > $ % '/ ( % # ) . % *
5 6
http://xkcd.com/c138.html Other C++ pointer-
pointer-like entities
& / .
. ' * / .
& ( (#
( (#
% & . % #D . .
% C / . ( (#
& . . % #D . .
& .( (#
. ( (#
& 8 . ' *
9B , ./ . ( (#
& 8 . / .

7 8

C++ pointer use A few more pointer-


pointer-related reminders
& E ' * & J KL ( (# ..
/ . ? '. ( ( * KL 0 / '? * 0
$ / F ' ( * & > %
& G "+ $ % . ( # % #
. HH I I HH HH +
8 % ' . *
? G F +
# ( 77 M #
. HH I I HH HH +
& $ ( / ( % A
? G +
. HH I I HH HH + & > % # . / .

9 10

Some pointer behaviors are undefined Lifetime of C++ variables


& / ( 6 / & $% # ) / ( ( % .
? + % # 0 %
?=G + ! & $% # ) / ( D /
& / . % / - .. / ( ( . 3 .= / # A
? G + - .. / 6 3. . / ( (
0 G? + ! & $% # ) / ( D # /
& B / (# % % % 8 # .. / '. * A
( 0 . / # % 8 ( ( .. / . ( 3/ 3 .
% ( # / . .
& $% # ) / ( . # (
& - ( % '( < ( * . ( ' ( . . / ( * A
% . H>L %' *+ > ? G F %N O+ (( ' ( . / ( *
% P# .<' *+
11 12
C++ dynamic lifetime management involves pointers But using pointers is problematical [Koenig/Moo, 2000]
& ? . P ' * Q ! ! & TN$O ( % % . U
" #
$ % 8 < ( . 6
' *+ ( % % . .
R & T (( U
& ( '* Q
( % ( 0 ( /D
? G . P ' " *+ ( . ' :*
& ' !
! ( ! & $ . ( T .
. HH I? I HH ? HH + ( .< U
? G S+ ) ( (( //
. HH I? I HH ? HH + # / % . ( (
+ ) ! # . % # .
+
R
13 14

Pointer issues 1: initialization Pointer issues 2: copying


& V 6 % # ' :* # / & T . N O
/. / . / / # /
V 6 % # . % % ( #D . U
% /. = . >? G >+ + +
& T 6 NAO. >? ' *+ + $ ! ,*
/ # % / ( N OU ? G + + ,*,*
+ +
>? + & ) ? >'
+ + ,*,*,*
. HH ? + *
& $ ( / / # (
& J/ . % < .
-. %
/ # %
>? G >+ + /' *+ +
( -/ %
/ 6 // . < % /'>? * Q + + R
( ' *
15 16

Pointer issues 3: pointer destruction Pointer issues 4: pointee destruction


& T N O & T N O
( ( < U . . /
$ ( < ( ( ( . . # % /N OU
% /' * Q > ? G >+ + R $ > ? G >+ + +
% '* Q + +
% . H> ?L %+ ! . HH ? + + %+ *
% P# .< ' > *+ + >? /'* Q
R $ > +
0
& J/ . % ( / .
F +
( R
// . ( . % ( # 6
/ (( ' *

17 18
Two common pointer misunderstandings http://xkcd.com/371
& . .
.
> ?. +
>. ? +
>. ?. W+
> ? +
& . ( = (
>? G +
/ ' < G + < :G + 77 < * Q
+ G >+ +
R
+ # 111 $*
9B ,

19 20

The moral of the story The real problem is more general


& $ ( ( . & ( ( ( ( # A
. % & , ( ( ( '. ( ( */ (
V . ( . / ( . ( ( # (
/ (
( ( # % # ( & 0 ( / . # ( (
( ( // . # 1
& 8 < . # (( . - .< 3 <. .
> < 3 . <=
.. # / .
, 0 ' ( % ( *
> . /
. > . ' . (( # .< ( .*
> / . # . % . .
> / / ( ) .
21 22

The resource management pattern Issues in releasing resources


& B . ( ( . . . & 1 < .
8 # .= / . A . % . # .= /
B . < // . = .
8 . ( . . . .. A
% ' ( . % # *
8 . ) ( > / . . < . #
& J . . % # / / & , .= . . / .
.= .. ! Q
& 0 ( >? ' > *+ 2 ! 3 ,
>? ' > *+ 4 ! / 3 ,
1 3 / .
>? W' > *+ 4 ! 5 3 ,
, 0 .< 3= / .< R
. . ' * Q
6 , 6 , ,
R
23 24
Today’
Today’s topics
. # ((
Selected Topics in Computer Programming #2
; ( . .
) ( . / ( ( (
Fine Points of C++ Pointers: 2 (( <
Dumb, Smart, and Smarter & , . . ( (
; 3 = ( 3 # ( 3 . 3
/ 2 ' ( *
& 5# # (
& 2 ((
' ) . = ( )*
. ( ( # (

26

Resource management algorithms/policies Some realities of resource management


& B . ( ( /. ( & 8 . . . ( ( /
8 'T J- . / ( U* A
> / . A > / 'T8) U* A
> . . ( # (( ( . ( . ( <
& 0 ( / . & 8 . . . ( ( (
- . .< # <
J / 8 ( / ' . C* %
J '. * . # . / ( <A
X '* # 0. # > . 3 # . ( <
% A
& . . . .
< / ( /
78 8 . . ( .

27 28

But resource management algorithms are separable Characteristics of handles


& .. ( / . (( < & . .
> . % ( . / . J ' < # / * . A
8 77 . / ( . . / ( - % 0 / . )
. . . ( ( % '. * & $ ( . /
& 1 . ( ( . (( # (
. . ( H. P L
. +
$ . ( (
. / . A & B$88 ' . .= 6 * . .
77 /
# % < ( .
' 88 ? .. . *A Y . #
. ( A
. / #D .
#D . ; ( . % ) # .<
29 30
The RAII pattern Recommended best practice
& $ ) & $ ( % # /
) < (( / . A . .=
) ( . A 78 8 % .. . / < .
J ( (# / . .. ( % / ( H>L ' V *+
. = # . & > V . ( +>( # .
& B % % 0 ( & $ ( ( # VZ .)
H>L ' > *+ 2 ! 3 0 & / ' . . (% 3 # *
H>L ' > *+ 0 / 3 0
H>L W ' > *+ 0 5 3 ,
5 < . # . ( 0 ( <

3 %
; / . . . . #
H>L
*
! $ !

31 32

But anonymous handles carry risks P HL is today’


today’s only standard handle
& /' H>L' > * ' * *+ & . H( ( L
/ . . / H>L .) & Y < . ' *
& = . /. & - P ) < .
, / ( '*# / . .) ( . /
, / ( # .) '*# / . /' * / ( . A
> # . (( // . # A
& > . ( # W # %
> / = # # ( # ' . *
+8 8 . P H>L . %
'* '* H>L' * /' *
9 '* '* H>L' * /' *
& $ P ) / ( ( .< %
( . % . %
'* H>L' * '* /' *
J ? ' / . * A
9: $ ! '* ) * J KL ' / . .. ( (# *
33 34

P HL’s conversions can be problematic


HL’ Other downsides to P HL
& > ) ( . >? P H>L . % & P HL . 0 . ( % ( .
78 8 . ) P H>L G >+ ( > / . 3 ( (# / P HL
8 P H>L ' > *+ ( ) .

& 8( . % # P HL . % / & - . . ) . P HL
. # . Q R+ > ) . = .
P H L ' *+ % . ( . A
P H L # G + * . . ( ' 88 '**
% /' P H L *+ ( . = %
/' *+ * & 2 # . ( . / 0
& , P HL ( TJ ( % / ( % . 0A
P H L ' N O*+ $ 0 J .. < ( %
. / NO . U 2 2 [

35 36
More standard handles are on the way (C++0X) These new handles aren’
aren’t so new
& = P HL & J . 6 # / / (
. ( / P HL % ( ( / . . 0 .
, % # < ( % ( ) ( (
% < 0 . + 2 % # % # 88/ ( 33#
) ( //
& P HL
V )
& 2 % % % . ((
( % . 78 8 # ,-
$ . ( % # . %
%% . / / ( & - . (( % (
& <P HL V / . ( ( .
. / ( P HL #
. P HL/ ( % <P H L . ( ( / . . <
V 88 % . . . . # 0 ( # % / 0
37 38

Passing handles to/from functions The letter-


letter-envelope/pimpl pattern, part 1 ( 0 / )
& > ( . / & . 0/ Q %! ; !
J ( . ( A # .
J . . / ) 0/ ' / ( * . +
% ' 0 * . +
& # / . #
; . . . ) . ( ! <<=> %
0 / ' 0 / F* G / + ?
> % /
0/ F G' 0/ F*G / +
& # % . \ 0/ '*G / + ?
> / #D . . . '% . .) *
%
78 8 P HL / . ( +
78 8 P HL P H ( L P( +
& # % / . ' 77 M* ( % R+
- #D . ( % . ' 8 8 ( % .) * /
= P HL # ( % #
39 40

The letter-
letter-envelope/pimpl pattern, part 2 ( 0 / ..)
..) Advantages of the pimpl pattern
& . 0/ ( Q & 8 . / ( ( (
# . $ . / # / .
( ' / * (
Y ( ( ( / ( ( %
/ ' / '/ ( .P ' * I I* * Q R
( ( ( . . .
\( '* Q /. ' / *+ R
& , ( 6 . ( .
% '. . ? # / 6P . * .
# ( % #
Q / '# / . / *+ R
5 . 6 # ( . /. .
% $
185 ? / +
& 5( . . )
R+ ; % % 9( (# . (
& 0/ 0/ ' / ( * / ( / .
P( ' ( '/ ( ** Q R & ( 0. /
& % 0/ ' 0 * . # # . /
Q P ( KL 'F 0N O 0 6 ' * *+ R . 0. / .
41 42
Other handle-
handle-based programming patterns Preventing clients from deleting pointees
& V P H% L . & . Q
%
P H% L %' M *+
\ '* Q R
> # . . . . +
\M' * ) # % < / . +
& / ( . ./.. # .
$ P HL# / A . P H L . P '* Q !
P H L ' ' * *+
. . # / ( +
& . . / . 3/ . #D . / R
& - / . ( ) .) R+
& . Q
% '*' ? * Q + R
R+
43 44

Pointing to a static-
static-lifetime object Executing cleanup code on block exit
& . P P Q & 78 8 . /' #* % % # .<
% '*'% . ?* . Q R % ( # 0.
R+
- . . . ( . / ( % #
& P H>L . P '* Q ! ( . . A
. > ' *+ V . P HL% # . (
P H>L ' F & Q P H% L ' .P. H % ? L' *
P P '* # '/ #*
*+ *+
R
R
& - ( # ' 88 * % . P HL%
/ 0 .
- . # (
45 46

Transactional (commit-
(commit-or-
or-rollback) semantics, part 1 Transactional (commit-
(commit-or-
or-rollback) semantics, part 2
& $% 0 . . . # .< 0 & V / ( .
Y . < -. Y $.. ( . ( . / .
$ 0 . , @ ! A 7) (@ ! -. Y . .
V@ . (# # .< ' *. /
& . / ( . %
/ 0 . . . # # / # # .<
0 . # ) & $ . ) ( . # .<
& # / # .< 0 . 8/ .. / D ( ' * . %
185 ? / G / ' I( / 0 I I I *+ # .< # . (( '. ( * .
-. Y . G , < Y ' /. / *+ 8/ .. / # /
. /. '/* % 3 % . ) ) # .
% # .<

47 48
Dealing with legacy code In sum
& -. . ( # & -( (( / (
. ( // . # ( .
& - ( ( ( .. & -( (( . ' % *
# . ( . . )
= P H>L ' *+ & -( (( <
' ' * *+ -.% $ % . . (%
' ' * *+ -/%
& -( (( % <
& (( ( # % . / %
/< =
# 3(
/ ] ( # < <
/ ] ( # < <
; % < # %
49 50

18;
Selected Topics in Computer Programming #2

Fine Points of C++ Pointers:


Dumb, Smart, and Smarter

! " # $ %