8/21/2019 06. Pointers
1/46
06. Pointers
1
8/21/2019 06. Pointers
2/46
2
Overview
1. Pointer Basics
2. Pointer as Function Parameter
3. Pointer and Array
4. Dynamic Memory Allocation
8/21/2019 06. Pointers
3/46
6.1 Pointer Basics
3
8/21/2019 06. Pointers
4/46
Pointers in C++ == Memory Addresses
Throuh !ointers" #e can directlyaccess$modi%y the data stored in the memory.
4
Introduction
8/21/2019 06. Pointers
5/46
To emulate !assin !arameters &y re%erence◦ C lanuae still uses !ointers to achie'e the e(ect
o% !ass)&y)re%erence.
Dynamic memory manaement◦ *e can decide ho# much memory s!ace #e #ant
at run-time.
5
What do we use pointers for in!!"
8/21/2019 06. Pointers
6/46
Continuous storae o% 1)&yte cells
ach cell has a uni,ue address ranin %rom - to2)1 in increasin order.◦ = / o% &its o% address$data the !rocessor can !rocess at a time
◦ 0n most com!uters #e use today" = 32 or 4
6
omputer Memory
… 100 101 102 103 104 105 106 107 108 109 110 …
Store 1 byte or 8 bits of data
Memory Address
8/21/2019 06. Pointers
7/46
ach 'aria&le is allocated an a!!ro!riateamount o% memory s!ace.
ach 'aria&le has a uni,ue memory address.◦ The address o% the rst cell &ecomes the address o%
the 'aria&le.
7
#aria$%es and MemoryAddresses
char ch; // 1 byteint x; // 32-bit integer (needs 4 bytes)
… 100 101 102 103 104 105 106 107 108 109 110 …
8/21/2019 06. Pointers
8/46
ach 'aria&le in the !roram is allocated ana!!ro!riate num&er o% cells in the memory%or storin data.◦ ch 1 &yte 1 cell◦ 4 &ytes 4 5continuous6 cells
The location o% each 'aria&le is determined&y the com!iler and the 07 5and thus may'ary %rom one eecution to another.6
8
#aria$%es and Memory Addresses
8/21/2019 06. Pointers
9/46
&" #hen a!!lied to a 'aria&le" yields the address o% the'aria&le.
9
&he address-of operator '&
char ch; // Assume ch occupies 1 byte at locatio 100int x; // Assume x occupies 4 bytes starti! at locatio 104
// "rit address of ch ad x as #e$adecimal umbers%cout
8/21/2019 06. Pointers
10/46
8n !arameter list◦ int #oo(int &x) $ % ◦ 8ndicates x is a re%erence !arameter
9se #ith 'aria&le in an e!ression◦ cout
8/21/2019 06. Pointers
11/46
Pointer varia$%es are 'aria&les that storememory addresses
int #oo;
int ' tr; // Declarin a !ointer 'aria&le tr oo; // tr stores the address o% #oo
◦ tr is a 'aria&le that holds an address o% a
'aria&le o% ty!e int.
11
Pointer #aria$%es
8/21/2019 06. Pointers
12/46
int y *;int 'tr;
tr &y;
12
Assi+nin+ Address of #aria$%es toPointers
51000
10002000
.
.
.
y
tr
51000
&2000
.
.
.
y
tr
tr stores t#e address of
'ariable y
or
tr poits to y
8/21/2019 06. Pointers
13/46
int y *;int 'tr;
tr &y; // tr gets the address o# y
cout
8/21/2019 06. Pointers
14/46
int x 1 y *;int '1 '2;
14
sin+ pointers to access varia$%es
51004
&2000
.
.
.
y
1
101000 x
&2004 2
1
2
5
y
10
x
&
&
Pictorial View Memory View
8/21/2019 06. Pointers
15/46
1 &x; 2 &y;
15
sin+ pointers to access varia$%es
51004
10002000
.
.
.
y
1
101000 x
10042004 2
1
2
5
y
10
x
Pictorial View Memory View
8/21/2019 06. Pointers
16/46
'1 .;'2 11;
16
sin+ pointers to access varia$%es
111004
10002000
.
.
.
y
1
71000 x
10042004 2
1
2
11
y
7
x
Pictorial View Memory View
8/21/2019 06. Pointers
17/46
2 1; // ot the same as '2 '1
17
sin+ pointers to access varia$%es
111004
10002000
.
.
.
y
1
71000 x
10002004 2
1
2
11
y
7
x
Pictorial View Memory View
8/21/2019 06. Pointers
18/46
cout
8/21/2019 06. Pointers
19/46
int x . y 11;int '1 '2;
1 &x;
2 &y;
'1 '2;
y 1;
cout
8/21/2019 06. Pointers
20/46
int x . y 11;int '1 '2;
1 &x;
2 &y;'1 y;
'2 x;
cout
8/21/2019 06. Pointers
21/46
int x . y 11 3 '1 '2;
2 &x; 2 &y;
'2 *;
1 2;
2 &;y ;
cout
8/21/2019 06. Pointers
22/46
int 'itr; // Pointer to integer
char 'ctr; // Pointer to char
double 'dtr; // Pointer to double
ectangle 'recPtr; // Pointer to a ectangle obect(see next lecture)
Pointer 'aria&les o% di(erent ty!es all store memoryaddresses. The !ointer ty!e tells the com!uter ho# to inter!ret
the data stored at the location !ointed to &y the!ointer.
22
Pointers to di2erent datatypes
ctritr Starti! #ere) treat t#e e$t
4 bytes as ite!er +reat 1 byte #ere as
c#aracter
8/21/2019 06. Pointers
23/46
int 'itr; // Pointer to integer
char 'ctr; // Pointer to char
double 'dtr; // Pointer to double
ectangle 'recPtr; // Pointer to class ectangle (see next lecture)
// Pointers o# di##erent tyes are not co,atible
// unless exlicitly conerted5
itr ctr; // 6o,ile-ti,e error
ctr itr; // 6o,ile-ti,e error
23
Pointers to di2erent data types
8/21/2019 06. Pointers
24/46
int y;
int 'tr1 &y;
int 'tr2 // tr2 stores the address
'tr3 7899; // tr3 stores the address
7899 is a !redened constant re!resentinaddress - 5it has the 'alue -6
*e can use or 7899 to indicate that a !ointer'aria&le is not !ointin to anythin 5no datacan &e stored at location -6
24
Pointer Initia%i3ation * 4u%%Pointer
Same as
int 'tr1; tr1 &y;
8/21/2019 06. Pointers
25/46
int y;int 'tr &y; // Correct
tr &y; // Correct'tr 1; // Correct'tr &y; // Com!ile)time error
tr 1; // Com!ile)time error
25
Pointer Initia%i3ation Pitfa%%s
8/21/2019 06. Pointers
26/46
Conce!ts o% com!uter memory and ho# dataare stored in the memory.
;no# ho# to declare !ointer 'aria&les
;no# ho# to use the address)o% o!erator 5&6and the dere%erence o!erator 5'6
9nderstand the di(erence &et#een co!yin
!ointers and co!yin the data !ointed &y the!ointers.◦ 1 2; 's '1 '2;
26
(ummary
8/21/2019 06. Pointers
27/46
6.5 Pointer as
unction Parameter
27
8/21/2019 06. Pointers
28/46
Pointers are !assed &y 'alue◦ The address store in one !ointer 'aria&le 5the actual
!arameter6 is co!ied to the another !ointer 'aria&le5the %ormal !arameter6.
Passin !ointers allo# us to emulate thee(ect o%
8/21/2019 06. Pointers
29/46
29
oid #oo(int ') $ ' ;%
// :n ,ain()int x 3;int 'tr &x;#oo(tr);cout
8/21/2019 06. Pointers
30/46
30
oid #oo(int ') $ ' ;%
// :n ,ain()int x 3;int 'tr &x;#oo(tr);cout
8/21/2019 06. Pointers
31/46
6.7 Pointer and Array
31
8/21/2019 06. Pointers
32/46
int #oo1; // =ssu,e int is 4 byte
32
8ow are 1, arrays stored inmemory"
800 801 802 803 804 805 806 807 808 809 810 811 812 813
Suppose array #oo starts at locatio 800
#oo #oo1 #oo2
Address of a array Address of its first elemet
-also o* as t#e base address.
+#e address of foo0 800
+#e address of foo 800
8/21/2019 06. Pointers
33/46
33
800 801 802 803 804 805 806 807 808 809 810 811 812 813
#oo #oo1 #oo2
Address of #ooi base address i 4
(e assume eac# 'alue of type int is 4 bytes i sie Array sie plays o role i determii! t#e address of a
array elemet%
+#e address of #oo2 800 24 808
+#e address of #oo> 800 94 836 +#e address of #oo1 800 1004 1200
8ow are 1, arrays stored in memory"
8/21/2019 06. Pointers
34/46
datatye array+:?@;
8% #e no# the &ase address o% array and the ty!e o% itselement" then #e can calculate the address o% any o% itselements.
For eam!le" address o% arrayindex is&ase address o% array + index > sieo#5datatye6
sieo#(tyeAna,e)◦ yields the num&er o% &ytes used to re!resent the 'alue o% ty!e
tyeAna,e.◦ e.." sieo#(int) yields 4
34
!! 9epresentation of 1, Arrays
8/21/2019 06. Pointers
35/46
An array in C++ is re!resented usin its &aseaddress.
e..
int array=1 arrayB1;double array62;
cout
8/21/2019 06. Pointers
36/46
An array is re!resented &y a &ase address
A !ointer 'aria&le stores address
A !ointer 'aria&le can &e used as i% it is anarray◦ *e can treat the address stored in a !ointer 'aria&le
as the &ase address o% an array
36
Pointer #aria$%es andArrays
8/21/2019 06. Pointers
37/46
37
Pointer #aria$%es andArrays
int 'tr #oo* $11 22 33 44 **;int bar3 $1 > !;
tr #oo; // tr !ets t#e base address of array #oo
// tr ca o* be used as a array// +#e 'alue stored i tr is used as t#e base addresscout
8/21/2019 06. Pointers
38/46
38
Pointer #aria$%es andArrays
int 'tr #oo* $11 22 33 44 **;
tr oo1; // tr !ets t#e address of #oo1
// ,irtually) tr is treated as a array i *#ic# its base// address is t#e address of #oo1%
// +#e 'alue stored i tr is used as t#e base addresscout
8/21/2019 06. Pointers
39/46
39
oid rint=rray(int tr int 7) $ #or (int i ; i < 7; iCC) cout
8/21/2019 06. Pointers
40/46
6.: ,ynamic Memory
A%%ocation
40
,ynamic Memory
8/21/2019 06. Pointers
41/46
Determine the si?e o% an array at com!ile timeint a*;
Can #e use the %ollo#in code determine the si?e o% an arrayat runtime@
int n;cin DD n
int an;
o. Placin n inside an #ill cause com!ile)time error
*e ha'e to use !ointers %or dynamic memory manaement◦ *hile your !roram is runnin" you can e!licitly allocate or reser'e
memory s!ace to store data
41
,ynamic MemoryMana+ement
8/21/2019 06. Pointers
42/46
int n;cin DD n;
int '#oo; // store the address o% the dynamic array
#oo neE intn;
// allocate a continuous memory s!ace %or n inteers
//assin the &ase address o% the array to
#oo
// #oo can &e used as an array
#or(int i ; i < n; iCC)
#ooi i;
// %ree u! A the s!ace occu!ied &y #oo
delete #oo;
42
sin+ neE with Arrays,ynamic Array A%%ocation
800 801 802 803 804 805 806 807 808 809 810 811 812 813
#oo #oo1 #oo2
Memory a%%ocation and
8/21/2019 06. Pointers
43/46
0!erator neE For allocatin memory s!ace to store data
◦ 8t reser'es a continuous chun o% memory s!ace
◦ 9!on success%ul allocation" it returns the memory
address o% the allocated s!ace◦ :ou should al#ays use a !ointer 'aria&le to sa'e
the memory address returned &y neE
0!erator delete For %reein u! the !re'iously allocated
memory s!ace
43
Memory a%%ocation anddea%%ocation Operator neEand
delete
8/21/2019 06. Pointers
44/46
int ''tEoF=rray; // 2D arrayint nu,#oE 4; // / o% ro#s o% 2D array
int nu,#6ol *; // / o% columns o% 2D array
// allocate sace #or tEoF=rray
tEoF=rray neE int'nu,#oE;
// allocate sace #or each tEoF=rrayi#or(int i ; i < nu,#oE; iCC) $
tEoF=rrayi neE intnu,#6ol;
555 555
// #ree u the sace occuied by tEoF=rray a#ter using it#or(int i ; i < nu,#oE; iCC) $
delete tEoF=rrayi;
delete tEoF=rray;
44
A%%ocate ,ynamic 5, ArrayOptiona%
8/21/2019 06. Pointers
45/46
45
Passin+ ,ynamic 5, Array to
unction Optiona%/' a #unction to rint the 2F array '/oid rint2F=rray(int ''tr int n int ,) $ #or (int i ; i < n; iCC) $ #or (int ; < ,; CC) $ cout
8/21/2019 06. Pointers
46/46
9nderstand the synta o% !assin !ointers to%unctions that acce!ts !ointers as !arameters
9nderstand ho# !assin !ointers to %unctionscan emulate the e(ect o%
9nderstand ho# 1D arrays are re!resented inC++ and its relationshi! #ith !ointers
;no# ho# to use neE to dynamically allocatememory s!ace
(ummary