Title: comp104 notes
1Review of pointers and dynamic objects
2Memory Management
- Static Memory Allocation
- Memory is allocated at compiling time
- Dynamic Memory
- Memory is allocated at running time
int a200
int n cin gtgt n an???
3Static vs. Dynamic Objects
- Static object
- Memory is acquired automatically
- Memory is returned automatically when object goes
out of scope
- Dynamic object
- Memory is acquired by program with an allocation
request - new operation
- Dynamic objects can exist beyond the function in
which they were allocated - Object memory is returned by a deallocation
request - delete operation
4Why pointers?
- Dynamic objects are implemented or realized
by pointers which are parts of low-level
physical memory - We dont like it, but can not avoid it.
- Low level languages directly manipulate them
- High level languages want to hide the pointers
(conceptually remove them)
5Pointers
- A pointer is a variable used for storing the
address of a memory cell. - We can use the pointer to reference this memory
cell
Memory address
1024
1020
10032
1024
100
a
int a int p
Integer a
Pointer p
6Getting an address address operator
int a100 a ? the address of a
Memory address
1024
1020
100
a
int a 100 cout ltlt a ? 100 Cout ltlt a ?
1024
7 Dereferencing Operator
- We can access to the value stored in the variable
pointed to by preceding the pointer with the
star operator (),
Memory address
1024
10032
1020
100
88
1024
a
p
int a 100 int p a cout ltlt a ltlt endl cout
ltlt a ltlt endl cout ltlt p ltlt " " ltlt p ltlt
endl cout ltlt p ltlt endl
p gives 100
8Pointer to pointer
int a int p int q
a 58 p a q p
a, p, and q are the same object whose value is
58! But q a is illegal!
9- An asterisk () has two usages
- In a definition, an asterisk indicates that the
object is a pointer. - char s // s is of type pointer to char
- (char s is possible)
- In expressions, an asterisk before a pointer
indicates the object the pointer pointed to,
called dereferencing - int i 1, j
- int ptr // ptr is an int pointer
- ptr i // ptr points to i
- j ptr 1 // j is assigned 2
- cout ltlt ptr ltlt j ltlt endl // display "12"
10Writing pointer type properly in C
int a int b
?
int a, b a, b are both integer pointers
int a, b a is integer pointer, b is just
integer!
typedefine int MyInt MyInt k
int k
I dont like this!
Recommended!!!
typedefine int IntPt IntPt a, b
11Summary
has two usages - pointer type
definition int a
int p -
dereferencing p is an integer variable
if p a has two usages
- getting address p a -
reference int b a
b is an alternative name for a First
application in passing parameters (swap example)
int a10 int b100 int p int q P a Q
b
p q p q
?
?
12Pointers and References
Reference (implemented as a (const) pointer) is
an abstraction, Not available in C, only in C.
13Pointer vs. Reference
- A pointer can be assigned a new value to point at
a different object, but a reference variable
always refers to the same object. Assigning a
reference variable with a new value actually
changes the value of the referred object. - int p
- int m 10
- int j m //valid
- p m //p now points at m
- int n 12
- j n //the value of m is set to 12. But j still
refers to m, not to n. - cout ltlt value of m ltlt m ltltendl //value of m
printed is 12 -
- n 36
- Cout ltlt value of j ltlt j ltlt endl //value of
j printed is 12 - p n
14- A reference variable is different from a pointer
int x10 int ref Ref x
int x10 int ref x
int ref
x
10
10
x
ref
ref
15- A constant reference variable v refers to an
object whose value cannot be changed through v. - int m 8
- const int j m
- m 16 //valid
- j 20 //compilation error
16Traditional Pointer Usage
- void swap(char ptr1, char ptr2)
- char temp ptr1
- ptr1 ptr2
- ptr2 temp
-
- int main()
- char a 'y'
- char b 'n'
- swap(a, b)
- cout ltlt a ltlt b ltlt endl
- return 0
Uese pass-by-value of pointers to change
variable values C language does not have call by
reference!
17Pass by Reference (better than pointers)
- void swap(char y, char z)
- char temp y
- y z
- z temp
-
- int main()
- char a 'y'
- char b 'n'
- swap(a, b)
- cout ltlt a ltlt b ltlt endl
- return 0
y, z are references, only names, not like ptr1,
ptr2 that are variables
18Pointers and Arrays
- Double faces of an array int a10
- a is the name of an array,
- a is also is a constant pointer to its first
element
19Pointers and Arrays
The name of an array points only to the first
element not the whole array.
2
a0
a
4
a1
6
a2
8
a3
22
a4
20Dereference of an array name
include ltiostreamgt Using namespace std void
main() int a5 2,4,6,8,22 cout ltlt a ltlt
" " ltlt a0 ltlt " " ltlt
(a0) ..." //main
2
a0
Result is 2 2 2
4
a1
6
a2
8
a3
22
a4
This element is called a0 or a
21Array name as pointer
- To access an array, any pointer to the first
element can be used instead of the name of the
array.
We could replace p by a
include ltiostreamgt Using namespace std void
main() int a5 2,4,6,8,22 int p
a int i 0 cout ltlt ai ltlt " " ltlt
p ...
2
2
a
22Pointers and constants
- A pointer two objects, pointer and the object
pointed to - const int i10 pointer to a constant
- Int const j20 constant pointer
- const int const k10 const pointer to const
23 dynamic objects
24Summary
Static variables (objects)
Dynamic variables (objects)
A (direct) named memory location
A static part (pointer) (indirect) nameless
memory location (dynamic part)
int a a 20
int pa pa new int pa 20
20
20
a
pa
static
dynamic
static
25Dynamic array
Simple dynamic variable
int p new int p 10 delete p
int p new int100 for (i1ilt100,i) pi
10 delete p
10
10
10
10
p
p
- delete two actions
- Return the object pointed to
- Point the pointer p to NULL
delete p is not sufficient for an array!!!
26A Simple List Example using a dynamic array
- concept of a list, e.g. a list of integers
- Print out info
- Empty test
- Search an element
- Insertion (at head, at end, any position)
- Deletion
-
- implemented
- by a static array (over-sized if necessary)
- int list1000 int size
- by a dynamic array
- int listsize int size
- by a linked list and more
-
-
27How to use a list?
int main() cout ltlt "Enter list size " int
n cin gtgt n int A new intn initialize(A,
n, 0) print(A, n) A addEnd(A,n,5) print(A,
n) A addHead(A,n,5) print(A, n) A
deleteFirst(A,n) print(A, n) selectionSort(A,
n) print(A, n) delete A
int A10000 int n
Nothing compulsary in programming, only style
matters!
28Initialize
- void initialize(int list, int size, int value)
- for(int i0 iltsize i)
- listi value
29Print out a list
- void print(int list, int size)
- cout ltlt " "
- for(int i0 iltsize i)
- cout ltlt listi ltlt " "
- cout ltlt "" ltlt endl
30Delete the first element
- // for deleting the first element of the array
- int deleteFirst(int list, int size)
- int newList
- newList new intsize-1 // make new array
-
- if(size) // copy and delete old array
- for(int i0 iltsize-1 i)
- newListi listi1
- delete list
-
- size--
- return newList
-
31Remark
Instead of A deleteFirst(A,n) we can also
just deleteFirst(A,n) if we define as a void
type function
void deleteFirst(int A, int size) A
newList
32Adding Elements
- // for adding a new element to end of array
- int addEnd(int list, int size, int value)
- int newList
- newList new int size1 // make new array
-
- if(size) // copy and delete old array
- for(int i0 iltsize i)
- newListi listi
- delete list
-
- newListsize value
- size
- return newList
33Add at the beginning
- // for adding a new element at the beginning of
the array - int addHead(int list, int size, int value)
- int newList
- newList new int size1 // make new array
-
- if(size) // copy and delete old array
- for(int i0 iltsize i)
- newListi1 listi
- delete list
-
- newList0 value
- size
- return newList