Title: Pointers and Dynamic Memory
1Chapter 5
- Pointers and Dynamic Memory
2Outline
The Pointer this dynamicClass Copy
Constructor The C Index Operator Matrices
Pointer Illustration Data Addresses in
Memory Declaring Pointer Variables Assigning
Values to Pointers Accessing Data with
Pointers Arrays and Pointers Operator
new Operator delete Illustrating the
Destructor Copy Constructor / Overloaded
Assignment Operator Declaration of dynamicClass
Objects
3- Pointers contain the address of data in
memory - Data is accessed by applying the
dereference operator
- Operators such as , , and apply to
pointers. - With such operators, pointers
can be used for algorithms involving
array traversal, but their primary
application is in the allocation and
maintenance of dynamic memory.
3
4Pointer Illustration
5Vertical and Horizontal View of Memory
6Data Addresses in Memory
7Declaring Pointer Variables
- Declare a pointer by stating the type followed by
the variable name, but with a "" added
immediately before the name. - The pointer ptr is a variable whose value is the
address of a data item of the designated type.
8Assigning Values to Pointers
- m is the address of the integer in memory. The
assignment statement - sets intPtr to point at an actual data item.
- The Figure illustrates the status of the two
variables from the declaration statement and the
resulting status after the assignment of a value
to intPtr.
int m 50, intPtr
intPtr m
9Assigning Values to Pointers
int x 50, y 100, px x, py y
10Accessing Data with Pointers
- The operator is called the deference operator.
It combines with pointer to allow access the
contents referenced by the pointer.
int x 50, y 100, px x, py y px
py 2 //assign to x the value y 2 102 py
2 //double value of y to 200 (px)
//increase x from 102 to 103
11The Relationship Between Arrays and Pointers
- An Array name is a constant pointer to the first
element in an array. It holds an address. - int vals 4, 7, 11
-
- cout ltlt vals // displays 0x4a00
- cout ltlt vals0 // displays 4
starting address of vals 0x4a00
12Arrays and Pointers
13The Relationship Between Arrays and Pointers
- an Array name can be used as a pointer constant
- int vals 4, 7, 11
- cout ltlt vals // displays 4
- A Pointer can be used as an array name
- int valPtr vals
- cout ltlt valPtr1 //displays 7
- cout ltlt (valPtr1) //displays 7
- cout ltlt valPtr //displays address of vals
14Pointers in Expressions
- Given
- int vals 4,7,11, valPtr
- valPtr vals
- What is valPtr 1 ?
- It means (address in valptr) (1 size of an
int) - cout ltlt (valPtr1) //displays 7
- cout ltlt (valPtr2) //displays 11
- Must use ( ) in expression
15Array Access
- Array elements can be accessed in many ways
- Use of subscript and offset
notation
16Array Access
- Conversion
- valsi is equivalent to (vals i)
- No bounds checking performed on array access,
whether using array name or a pointer
17Pointer Arithmetic
18Pointers and Class Types
- The dereference and select operator, -gt, is used
only with pointers to objects - It accesses an object member by using the pointer
name and member name as operands, rather than the
deferenced object in parentheses and the dot
operator
Rectangle box (2, 5), rectPtr // a 2 by 5
rectangle and a pointer rectPtr box //set
rectPtr to point at box cout ltlt(rectPtr).area()
//area of the box is 10 cout ltltrectPtr-gtarea() co
ut ltlt(rectPtr).setSides(7,10) //update
dimension of box cout ltltrectPtr-gtsetSides(7,10)
19Dynamic Memory Allocation
- Static memory allocation the compilation process
creates an executable file in which the memory
requirements for each variable and object are
defined. - Dynamic memory allocation A program can allocate
storage from additional memory resource, heap,
for a variable while it is running - Use the new operator to allocate memory
- double dblPtr
- dblPtr new double
- new returns the address of a memory location if
it is successful or 0 ( NULL ) if not.
20Dynamic Memory Allocation
- You can use new to dynamically allocate an
array - double arrayPtr
- cout ltlt "How many real numbers? "
- cin gtgt count
- arrayPtr new doublecount //count is a
variable! - You can use subscript or offset notation to
access the array elements. - for (int i 0 i lt count i)
- arrayptri i i
- or
- for (int i 0 i lt count i)
- (arrayptr i) i i
21Operator new
p new time24 // p is 0000
(midnight) q new time24(8, 15) // q is
815 AM
22Releasing Dynamic Memory
- Use delete to free dynamic memory
- delete fPtr //single element
- Use to free a dynamic array
- delete arrayPtr //array of elements
- Only use delete with dynamically allocated
memory!
23Classes using dynamic memeory
- Memory management dynamically allocate
deallocate memory from heap - Memory leak allocate without corresponding
deallocate - Dynamic allocation done by constructor
- Dynamic deallocation done by destructor
- Example Page 235, dynamicClass
- Constructor
- Example 5-8
24 Illustrating the Destructor
- Example page 238-239, Program 5-1
25Copy Constructor / Overloaded Assignment Operator
- Assignment and initialization for objects that
allocate dynamic data, the class must provide
special member functions, called copy constructor
and the overloaded assignment operator, that
enable the run time system to execute the
operations.
26Declaration of dynamicClass Objects
27Action of using the default assignment operator
for the statement objBobjA
28Design of the overloaded assignment operator,
29The this Pointer
- this predefined pointer available to a classs
member functions - Always points to the instance (object) of the
class whose function is being called - Is passed as a hidden argument to all non-static
member functions - Can be used to access members that may be hidden
by parameters with same name
30The Pointer this
31this Pointer Example
- class SomeClass
-
- private
- int num
- public
- void setNum(int num)
- this-gtnum num
- ...
32dynamicClass Copy Constructor Algorithm
PP 247 Program 5-2
33Overloaded Operator
- Can create classes that behave like arrays,
provide bounds-checking on subscripts - Must consider constructor, destructor
- Overloaded returns a reference to object, not
an object itself
34The C Index Operator
arri 30 // arri is the //address into
which 30 is copied t arri 4 // add 4 to
the //value of the element at arri
35- vector implementation - The
miniVector class illustrates the key
points. 1) It allocates dynamic memory using
destructor copy
constructor overloaded
assignment operator 2) It implements
push_back() Therefore it must
control vector capacity in order to
minimize dynamic memory reallocation. 3) It
allows access to elements by using an
index Therefore
the class implements an overloaded index
operator
35
36- Two dimensional arrays in C - have the
same problems as one-dimensional
arrays 1) fixed size 2) no size
attribute 3) If it is a function argument, it
is necessary to specify the number of
columns as a constant.
36
37Matrices
- A Matrix is a two-dimensional array that
corresponds to a row-column table of entries of a
specified data type. - Matrices are referenced using a pair of indices
that specify the row and column location in the
table.
Example The element mat03 is 2 The element
mat12 is 4.