Title: What have we covered so far
1What have we covered so far?
- File streams in C
- String manipulation (C strings)
- Data Abstraction and Data Encapsulation (C
struct and class types) - ADT Unsorted List
- ADT Sorted List (inc. binary search)
- ADT Stack
- Introduction to C exceptions (throw and
try-catch statements) and C templates
2Lecture 9C Pointers
memory
address
int intPointerint anotherPointerint
oneIntPtr, twoInt
33
int alpha 8
intPointer alpha
25
is called address-of operator
intPointer 25
is called dereference operator
3Dynamic Allocation of Memory (concept)
Dynamic Allocation is allocation of memory space
for a variable at run time (as opposed to static
allocation at compile time)
Program
Data allocated statically at compile time
Free Store (Heap) used for dynamic allocation
Memory
4Dynamic Allocation of Memory in C
address
memory
int intPointer
intPointer new int
150
Alternativellyint intPointer new int
?
part of the heap
0150
5Dynamic Allocation of Memory in C (more)
bool truth NULLfloat money NULL
- We assign NULL to a pointer if we want a pointer
to point tonothing - NULL is equivalent to the int value 0
- NULL is defined in the header file ltcstdlibgt
6Dynamic Allocation of Memory in C (more)
truth new booltruth truemoney new
float money 33.46float myMoney new float
7Dynamic Allocation of Memory in C (more)
Pointer variables can be compared for equality
and can be assigned one to another as long as
they point to variables of the same data type.
8Garbage Collection
delete myMoney
Note that delete does not delete the pointer
variable, but the variable (i.e. the memory) to
which the pointer points.
myMoney money
myMoney
myMoney
33.46
9Array Names and Pointers
char alpha20char alphaPtrchar
letterPtrvoid Process(char arrayParam) alph
aPtr alphaletterPtr alpha0Process(alpha
)
Structs (classes) and Pointers
struct MoneyType int euros int
centsMoneyType moneyPtr new
MoneyTypemoneyPtr -gt euros 1000 // or
(moneyPtr).euros1000 moneyPtr -gt cents 50