Upload
oleksiy-kovyrin
View
221
Download
0
Embed Size (px)
Citation preview
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 1/9
Selected Topics in Computer Programming #2
Selected Topics in Computer Programming #2Selected Topics in Computer Programming #2
Fine Points of C++ Pointers:Pointers:
Dumb, Smart, and SmarterDumb, Smart, and Smarter
! "#$%
3
A little about meA little about me& $'()*+,-'.(..*
& /((/
& 0.#.(
1(-.+%/
2+3(%
,3(((///.(+
/(/..((.
& $1(#.44"+(%35-
.677.((
& .776.
& /#%0.
8(#.(/
(((9 #
((##:
4
TodayToday’’s topicss topics
& .#((
;(..
) (./(((
& 2((<
& ,..((
;3=(3#(3.3
2'(*
& 5##(
& 2((
5
Quick review of pointer nomenclatureQuick review of pointer nomenclature
& 1%> >?
. >? % '#*%/>
& $%#%%#
$(3%3#3(#
& $ %%/%#
@<%#) %%%
<%/%.#.(
%'%0./?*
& $ %#%.
%/(%#
$%#/>? />
6
The pointerThe pointer--pointee relationshippointee relationship
& $%#
& $%(
$% '/().%*A
$% '/(%#).%*
A pointer variableof pointer type >?
A pointer valueof pointer type >?
A pointee of type >
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 2/9
7
http://xkcd.com/c138.htmlhttp://xkcd.com/c138.html
9 B,
%%C
8
Other C++ pointerOther C++ pointer--like entitieslike entities
& /.
.'*/.
& ((#
((#
& .%#D..
/.((#
& ..%#D..
& .((#
.((#
& 8.'*
./.((#
& 8./.
9
C++ pointer useC++ pointer use
& E'*
/. ?'.((*
$/ F'(*
& G "+
.HHIIHHHH+
? G F +
.HHIIHHHH+
? G+
.HHIIHHHH+
10
A few more pointerA few more pointer--related remindersrelated reminders
& JKL ((#..
KL0 /'?*0
& > %
$%.(#%#
8 %'.*
#(77M#
& $(/(%A
& >%#./ .
11
Some pointer behaviors are undefinedSome pointer behaviors are undefined
& /(6/
?+
?=G+ !
& /.%/
?G+
0G?+ !
& B/(#%%%
(0./#%
%(#/..
& -(% '(<(*
%.H>L%'*+>?GF%NO+
%P#.<'*+
12
Lifetime of C++ variablesLifetime of C++ variables
& $%#)/( (%.
%#0%
& $%#)/(D/
-../((.3.=/#A
-../63.. /((
& $%#)/(D#/
8#../ '.*A
8((../.(3/3.
& $%#)/(.#(
.('(. . /(*A
(('(. /(*
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 3/9
13
C++ dynamicC++ dynamic lifetimelifetime management involves pointersmanagement involves pointers
& ? .P' *Q !!
"#
$%
' *+
R
& (' *Q ? G .P'" *+
&' !
!(!
. HHI? IHH?HH+
? GS+ )
. HHI? IHH?HH+
+ ) !
+
R
14
But using pointers is problematicalBut using pointers is problematical [Koenig/Moo, 2000][Koenig/Moo, 2000]
& TN$O(%%.U
8< (.6
(%%..
& T((U
(%(0(/D
(.':*
& $ .( T.
(.<U
(((//
#/%.((
#.%#.
15
Pointer issues 1: initializationPointer issues 1: initialization
& V6%#':*#/
/././/#
V6%#.%
%/.=.
& T6NAO.
/#% /(NOU
>?+ &)? >'
.HH? + *
& J/.%<.
/#%
(
/6//.<
('*16
Pointer issues 2: copyingPointer issues 2: copying
& T.NO
/
%(#D.U
>? G>+ ++
>? '*+ +$!,*
?G + +,*,*
+ +
+ +,*,*,*
& $(//#(
-.%
>? G>+ +/'*++
-/%
%/'>? *Q ++R
17
Pointer issues 3: pointer destructionPointer issues 3: pointer destruction
& TNO
((<U
$(<(((..
%/'*Q>? G>+ +R $
%'*Q%.H>?L%+ !
%P#.<'>*+ +
R $
& J/.%(/.
(
//.(.%(#6
/(('*
18
Pointer issues 4: pointee destructionPointer issues 4: pointee destruction
& TNO
../
#%/NOU
>? G>+ ++
+ + .HH? + +%+*
>? /'*Q
>+
0
F+
R
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 4/9
19
Two common pointer misunderstandingsTwo common pointer misunderstandings
& ..
.
> ?. +
>. ? +
>. ?. W+
> ? +
& .( =(
>?G+
/'<G+<:G+77<*Q
+G>++
R
+ # 111$*
20
http://xkcd.com/371http://xkcd.com/371
9 B,
21
The moral of the storyThe moral of the story
& $ ((.
.%
V.(.
/(
((#%#(
((//.#
& 8<.#((.
><3.
..#/.
>./
.
>/.#.%..
>//().22
The real problem is more generalThe real problem is more general
& ((((#A
& ,((('.((*/(
/(.((#(
& 0(/.#((
1
-.<3<..
<=
,0'(%(*
>.'.((#.<(.*
23
The resourceThe resource management patternmanagement pattern
& B. ((...
8# .= /.A
8.( ... .. A
8 .) (
& J..%#//
.=..!
& 0(
13/.
,0.<3=/.<
24
Issues in releasing resourcesIssues in releasing resources
& 1 < .
.%.#.=/
B.<//.=.
% '(.%#*
>/..<.#& , .=../.
Q
> ? ' >*+ 2! 3 ,
> ? ' >*+ 4 ! / 3,
> ? W' >*+ 4! 5 3,
R
..' * Q 6 , 6 , ,
R
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 5/9
Selected Topics in Computer Programming #2Selected Topics in Computer Programming #2
Fine Points of C++ Pointers:Pointers:
Dumb, Smart, and SmarterDumb, Smart, and Smarter
').=()*
.((#(
/
26
TodayToday’’s topicss topics
.#((
;(..
) (./(((
2((<
& ,..((
;3=(3#(3.3
2'(*
& 5##(
& 2((
27
Resource management algorithms/policiesResource management algorithms/policies
& B.((/.(
>/.A
>. .(#((
& 0(/.
-..<
J/
J'.*.#
X'*#0.#
& ....
788..(.
28
Some realities of resource managementSome realities of resource management
& 8...((/
8'TJ-./(U*A
>/'T8)U*A
(.(.(<
& 8...(((
#<
8(/'.C*%
./(<A
>.3#.(<
%A
</(/
29
But resourceBut resource management algorithms are separablemanagement algorithms are separable
& ..(/.((<
>.%(./.
877./(../(
...((% '.*
& 1.((.((#
..
$.((
./.A
#%<(.
'88?...*A
./#D.
#D.
30
Characteristics of handlesCharacteristics of handles
& ..
J '<#/*.A
-%0 /.)
& $ ( ./
( (H.PL
.+
& B$88 '..=6*..
77/
Y.#
.(A
;(.%)#.<
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 6/9
31
The RAII patternThe RAII pattern
& $ )
) <((/ .A
) ( .A
J ((#/...(
.=#.& B%%0(
H>L' >*+ 2!30
H>L' >*+ 0 / 30
H>LW' >*+ 0 5 3,
3%
H>L
*
! $!
32
Recommended best practiceRecommended best practice
& $( %#/
..=
788%.../ <.
%/(H>L'V*+
& > V.(+> (#.
& $((#VZ.)
& /'..(%3#*
5<.#.(0(<
;/ .. ..#
33
But anonymous handles carry risksBut anonymous handles carry risks
& /'H>L'>*'**+
/../ H>L.)
& =./.
,/( '* #/..)
,/(#.)'* #/./'*
& >.(#W#%
/'*'*H>L'* '*
/'*H>L'*'*'*
/'*H>L'* '*'*
9:$!'* )*
9
34
PHLPHL is todayis today’’s only standard handles only standard handle
& .H((L
& Y<. '*
& -P)<.
(./
/(.A
>#.((//.#A
>/=##(#'.*
+88.PH>L.%
& $ P)/((.<%
(.%.%
J?'/.*A
JKL '/... ((#*
35
PHLPHL ’’s conversions can be problematics conversions can be problematic
& >)(.> ?PH>L .%
788.) PH>LG>+ (
8 PH>L'>*+ (
& 8(.%#PHL .%/
.#.Q R+
PHL'*+
PHL#G+ *
%/'PHL*+
/'*+ *
& ,PHL (
PHL'NO*+ $0
. /NO
36
Other downsides toOther downsides to PHLPHL
& PHL .0.(%(.
>/.3((#/PHL
).
& -..). PHL
>).=.%.(.A
..('88'**
(.=%
& 2#.(./0
TJ(%/(%.0A
J..<(%
.U 22[
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 7/9
37
More standard handles are on the way (C++0X)More standard handles are on the way (C++0X)
& =PHL
.(/PHL
,%#<(%(
%<0.+
& PHL V)
(%.
$.(
%%.//(
& <PHL
./(PHL
.PHL /(%<PHL
V88%....38
These new handles arenThese new handles aren’’t so newt so new
& J.6#//(
%((/..0.
) ((
2%#%#88/(33#
)(//
& 2%%%.((
788#,-
%#.%
& -.((%(
V/.((.
#
.((/..<
#0(#%/0
39
Passing handles to/from functionsPassing handles to/from functions
& >(./
J(.(A
J../)
& #/. #
;...).
>%/
& #% .
>/#D...'%..)*
788PHL /
788PHL
& #%/. '77M*(%
-#D.(%.'88(%.)*/
=PHL #(%#40
The letterThe letter--envelope/pimpl pattern, part 1 (envelope/pimpl pattern, part 1 (0/0/))
& .0/Q %!;!
#.
0/'/(*.+
%'0*.+
(!<<=>%
0/'0/F*G/+ ?
0/FG'0/F*G/+
\0/'*G/+ ?
%
.(+
PH(LP(+
R+
41
The letterThe letter--envelope/pimpl pattern, part 2 (envelope/pimpl pattern, part 2 (0/..0/..))
& .0/(Q
#.
('/(*
/'/'/(.P'*II**QR
\('*Q/.'/*+R
%'..?#/6P.*.
Q/'#/./*+R
% $
185?/+
R+
& 0/0/'/(*
P('('/(**QR
& %0/'0*.
QP( KL 'F0NO06'**+R42
Advantages of the pimpl patternAdvantages of the pimpl pattern
& 8./(((
$./#/.
Y(((/((%
(((...
& ,(6.(. #(%#
5.6#(./..
& 5(..)
;% %9 ((#.(
/(/.
& (0./
##./
.0./.
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 8/9
43
Other handleOther handle--based programming patternsbased programming patterns
& VPH%L .
PH%L%'M*+
>#...
\M'* )#%<
& /(../..
$PHL #/A
..#/(
& ../.3/.#D./
& -/ .( ).)
44
Preventing clients from deleting pointeesPreventing clients from deleting pointees
& .Q
%
\'*QR
.+
/.+
#.
.PHL.P'*Q !
PHL''**+
+
R
R+
& .Q
%'*'?*Q+R
R+
45
Pointing to a staticPointing to a static--lifetime objectlifetime object
& .PPQ
%'*'%.?*.QR
R+
& PH>L.P'*Q !
.>' *+
PH>L 'F
PP'*
*+
R
46
Executing cleanup code on block exitExecuting cleanup code on block exit
& 788./'#*%%#.<
%(#0.
-...(./(%#
(..A
V.PHL %#.(
& QPH%L'.P.H%?L'*
#'/#*
*+
R
& -(#'88*%.PHL%
/0.
-.#(
47
Transactional (commitTransactional (commit--oror--rollback) semantics, part 1rollback) semantics, part 1
& $%0...#.<0
Y.<-.Y
$0.,@!A7)(@!
V@.(#
& /0...#
0.#)
& #/#.<0.
185?/G/'I(/0III*+
-.Y.G,<Y'/./*+
./.'/*%3%.
%#.<
48
Transactional (commitTransactional (commit--oror--rollback) semantics, part 2rollback) semantics, part 2
& V/(.
$..(.(./.
-.Y ..
#.< '*./
./(.%#/##.<
& $.)(.#.<
8/../D( '*.%
#.<#.(( '.(*.
8/../#/
) )#.
8/14/2019 2008-04 Pointers
http://slidepdf.com/reader/full/2008-04-pointers 9/9
49
Dealing with legacy codeDealing with legacy code
& -..(#
.(//.#
& -(((..
#
=PH>L'
*+''**+ -.%$
''**+ -/%
& (((#%./%
#3(
/](#<<
/] (#< <
;%<#%
50
InIn sumsum
& -(((/(
(.
& -(((.'%*
.(..)
& -(((<%..(%
& -(((%<
/<=
Selected Topics in Computer Programming #2Selected Topics in Computer Programming #2
Fine Points of C++ Pointers:Pointers:
Dumb, Smart, and SmarterDumb, Smart, and Smarter
! "#$%
18;