Title: Pointers
1Chapter -- 7
- Pointers Dynamic
- Memory Management
2- Pointer types
- Call by reference using pointers
- Pointers as arrays
- Arrays as pointers
- Pointer arithmetic
- Uninitialized pointers
- String problems
- Command line arguments
- Dynamically allocated memory
3Pointer Types
- In C, variables have a
- name
- type
- value
- location in memory (or address)
4Example
- int alpha 20, beta 30
- Address Memory cell Program identifier
- 2000 alpha
- 2001 beta
20
30
5Pointer Types
- ltTypegt ltidentifiergt
- int p // p is a pointer to an integer
- The placement of asterik is not important
- int p
- int p
- int p
- Note int p, q is equivalent to int p int
q
6The Address-of Operator
-
- Address Memory Cell Program identifier
- int q 20 2000 q
- int p 3000 p
- p q 3000 p
20
???
2000
7- p 30 is equivalent to q 30
- Note
-
- int i 10, p i // correct
- p i / as a statement by
- itself is wrong /
8- if
- p 20
- q 30
- then p q
- p 20 30
- q 30
9 p q p 20 q 30
10The Null Pointer
-
- During program execution, a pointer
- variable can be in one of the following
- states
- 1. It is unassigned
- 2. it points to some data object
- 3. It contains the pointer constant 0
- (or null pointer)
-
11Pointer value can be tested for if (p 0)
... Instead of constant 0, it is better to
use a constant identifer -- NULL include
ltstddef.hgt ... p NULL ...
12Call-by-reference using Pointers
- include ltiostream.hgt
- const double PI 3.1416,
- RHO 45.2377
- void initialize( int x, int y)
- void main( )
-
- intval1, val2
- initialize(val1, val2)
- cout ltlt val1 ltlt val1 ltlt , val2 ltlt
val2 -
13- void initialize( int a, int b)
-
- double diam
- int temp
- cout ltlt Please enter diameter
- cin gtgt diam
- temp PI diam (RHO/2-1)
- a temp
- b temp PI
14Pointers as Arrays Arrays as Pointers
- int a 3 , p
- p a
- / The same as
- p a 0 /
- a 0 2
a
address
a 0 2 2000
a 1 2002
a 2 2004
p
15- p 10 is equivalent to a 0 10
- a 20 is equivalent to a 0 20
- Note
- a p is not allowed
- Array name is a constant pointer
16Pointer Arithmetic
int vec 3 , vptr vec 0 5 vec 1
10 vec 2 15 vptr vec vptr
5 (vptr 1) 10 (vptr 2) 15 vptr
2 10 / is equivalent to (vptr) 2 /
17Example
Zero out a 100-element integer array int vec
100 , ptr for (ptr vec ptr lt vec 100
ptr) ptr 0 int vec 100 , i 0 while
(i lt 100) veci 0 i
18Pointer Arithmetic
The following arithmetic operators can be
applied to pointer variables - -
- - The following formula can used to
find the pointerss address p p si where
p is the new pointer value p is the current
pointer value s is the size (in bytes) of type
that is pointed to i is the value added to the
pointer
19Example
int i 10, ip i if i is located at
address 210 in memory, then ip makes ip to
point to location 212
20Example
int a 100 , p a p 10 / Increment
address p and then dereference it. That is,
dereference a 1 /
21Example
struct block float height, width int
color block blk, p blk (p).color
5 / Note that . has higher precedence
than / We can write the above statement as
follows p -gt color 5
22String Problems
- char str
- str This is a string. // This is valid
- char str 20
- str This is a string. / Not allowed because
str - is a constant pointer /
23- Do not try
- char str
- cin gtgt str
- // Some data might be overwritten
- To read a string
- char str 20
- cin gtgt str // Do not type more than 19
char - Or
- char str20
- cin.getline( str, 20)
24// Bad String_Scan( ) // String is passed by
value void String_Scan( char s) if (s
A) cout ltlt A found in string!
s //Corresponding bad main( ) void main( )
char in_string 100 , cursor
cin.getline(in_string, 100) cursor
in_string while(cursor 0 ! \0)
String_Scan(cursor)
25// Better String_Scan( ) // String is passed by
reference void String_Scan( char s) if
(s A) cout ltlt A found in string!
(s) //Corresponding better main( ) void
main( ) char in_string 100 , cursor
cin.getline(in_string, 100) cursor
in_string while(cursor 0 ! \0)
String_Scan(cursor)
26Command-line Arguments
- The user may pass parameters to main by typing
parameters (called command-line arguments) on the
operating systems command line. - Example
- c\ prog one two three
- Three parameters are passed one, two, and
three.
27C passes two parameters to the main
function argc The count of command-line
arguments (including the program
name). argv A vector of character strings, one
for each command-line argument (including the
program name).
28To access these arguments the main function
declares them as follows int main ( int argc,
char argv ) . . .
29argv 0 p r o g
\0 argv 1 o n e
\0 argv 2 t w o
\0 argv 3 t h r e
e \0
30Example
- include ltiostream.hgt
- include ltctype.hgt
- void main (int argc, char argv )
- char ch
- if (argc lt 3)
- cout ltlt Invalid number of parameters \n
- else
- while ((cin.get (ch) ! EOF)
- continue...
-
31continue... if (toupper(ch)
toupper(argv 1 0 ) cout ltlt argv 2 0
else cout ltlt ch c\ charconv E
lt file1.dat gt file1.new
32Dynamically Allocated Memory
- There are three general classifications of
program data - static data (retains its value from call to
call) - automatic data (destroys when function exits)
- dynamic data (its life time is controlled during
execution by means of special program
instructions)
33Allocation Deallocation
The new operator new lttype namegt or new
lttype namegt ltint expressiongt (to allocate an
array)
34if memory space is available then an
uninitialized object of the appropriate size is
allocated and a pointer to the object is
returned else the null pointer 0 is
returned Examples int intptr new
int char str new char 51
35The delete operator delete lt pointer gt The
object pointed to by lt pointer gt is deallocated
and lt pointer gt is considered unassigned. Examp
le delete intptr // Single int object
deallocated delete str // Entire array
deallocated
36Examples
float floatPtr new float char
nameStr new char 7 Free Store
floatPtr nameStr 0 1 2 3
4 5 6
37Program
/ This program inputs and stores an arbitrary
number of integer values by using dynamic array
allocation. / include ltiostream.hgt include
ltstddef.hgt // For NULL void sort(int ,
int) int main( ) int vec, numItems,
i continue...
38continue... cout ltlt Enter of items to
sort cin gtgt numItems vec new int
numItems if (vec NULL) cout
ltlt Not enough memory \n return 1
cout ltlt Enter ltlt numItems ltlt integers
\n for (i 0 i lt numItems i)
cin gtgt vec i continue...
39continue... sort(vec, numItems) cout ltlt
\nSorted list \n for (i 0 i lt
numItems i) cout ltlt vec i ltlt \n
delete vec return 0
40Inaccessible Objects
An inaccessable object is a dynamic object
without any pointer left pointing to it.
41Example
intPtr Before -77 Instruction intP
tr new int
intPtr After -77
42Example
intPtrA intPtrB -77
8 Instruction intPtrA intPtrB
intPtrA intPtrB -77 8
43- A memory leak occurs when a program allocates
dynamic data but never deallocates that data from
the free store.
44Dangling Pointers
A dangling pointer is a pointer that still
points to an object that has been deallocated.
45Example
intPtrA intPtrB Before
-77 Instruction delete intPtrA
intPtrB After
?