Title: Wavelet transform And Its Applications to Image Processing
1Writing a Good Program 6. Pointers and Arrays
2Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
6.1 Pointers
3Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
How memory is used in C?
- The whole big piece of memory is divided into 4
areas - Code Space - for the storage of program code
- Stack - for the storage of local variables,
passed parameters. - Global Name Space - for the storage of global
variables - Free store - for the storage of dynamically
created data
4Computer Programming and Basic Software
Engineering
How memory is used in C?
6. Pointers and Arrays
5Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
What is the Address of a Variable?
- A variable is a storage space in memory.
- Every variable has a memory address.
Each byte has an addressEach variable has the
starting-byte address
Variables char a int b short int c bool d
Memory
30
0A
21
3A
51
44
20
00
Address
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
The character '0'
a 30 Address of a 0100 b 0A 21 3A
51 Address of b 0101 c 44 20 Address of c
0105
All values written in hexadecimal but binary in
reality
6Computer Programming and Basic Software
Engineering
What is the Address of a Variable?
6. Pointers and Arrays
- In C, the symbol is used to indicate the
address of a variable.
include ltiostreamgt using namespace std int
main() unsigned short shortVar 5 unsigned
long longVar 65535 long sVar -65535 cout
ltlt "shortVar\t" ltlt shortVar cout ltlt "\tAddress
of shortVar\t" cout ltlt shortVar ltlt
"\n" cout ltlt "longVar\t" ltlt longVar cout ltlt
"\tAddress of longVar\t" cout ltlt longVar ltlt
"\n" cout ltlt "sVar\t\t" ltlt sVar cout ltlt
"\tAddress of sVar\t" cout ltlt sVar ltlt
"\n" return 0
The addresses of shortVar, longVar and sVar
7Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
What is the Address of a Variable?
- Variable and address of a variable are different.
8Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
What is a Pointer?
- In many situations, we want to store the address
of a variable into a particular memory location.
Variables char a int b (address of a) pa
Memory
10
0A
21
3A
51
00
01
00
00
Address
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
pa is the pointer of a
- pa is a variable that can store the address of a.
- In C, every address has 4 bytes. So we need to
reserve 4 bytes of memory to store the address of
a .
9Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
What is a Pointer?
- In C, every variable needs to have its type
declared. - int abc // means abc belongs to the type of
integer. - CAT Felix // means Felix belongs to the class
CAT
- If we want to declare the type of pa, which
stores the address of a variable a, how should we
do it? - ? How about address pa
Not good enough, since it does not tell the
nature of a
? How about (address of a character) pa
Better, but look too clumsy
10Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
What is a Pointer?
- C uses an elegant way to solve the problem (but
need some time to understand!). - It introduces a symbol .
- ? means the content of an address.
pa's content is an address, the memory content of
that address is a character
Variables char a int b char pa
Memory
10
0A
21
3A
51
00
01
00
00
Address
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
- char pa indicates that the memory content of the
address. stored in pa is a character. pa is
indirectly declared to be an address of character.
11Computer Programming and Basic Software
Engineering
What is a Pointer?
6. Pointers and Arrays
- We can modify the content of a memory location
using pointer.
Variables char a int b char pa
Memory
30
0A
21
3A
51
00
01
00
00
Address
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
char pa, a0x30 // 48 cout ltlt a // a
'0' pa a // pa 0100 cout ltlt pa // pa
30 pa 49 // a '1' cout ltlt a
We modify a indirectly by using its address
12Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
include ltiostreamgt using namespace std typedef
unsigned short int USHORT int main() USHORT
myAge // a variable USHORT pAge 0// a
null pointer, pAge0, not pAge0 // Dont let
it become wild pointer (point to unknown) myAge
5 cout ltlt "myAge " ltlt myAge ltlt "\n" pAge
myAge // assign address of myAge to pAge cout
ltlt "pAge " ltlt pAge ltlt "\n\n" cout ltlt "pAge
7\n" pAge 7 // pAge7, not pAge7, sets
myAge to 7 cout ltlt "pAge " ltlt pAge ltlt
"\n" cout ltlt "myAge " ltlt myAge ltlt
"\n\n" cout ltlt "myAge 9\n" myAge 9 cout
ltlt "myAge " ltlt myAge ltlt "\n" cout ltlt "pAge "
ltlt pAge ltlt "\n" return 0
13Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
14Computer Programming and Basic Software
Engineering
Why Pointer? - Using Free Store
6. Pointers and Arrays
- Pointer allows us to handle the memory in Free
Store. - The memory Free Store is opened to all functions.
- Pointer helps us to identify the part of memory
in Free Store that is being used by a particular
function or object. - To use the memory in Free Store
- 1. Make a claim to the system how much memory is
required. - 2. System allocates a memory space with big
enough size. - 3. System returns a pointer value which is the
starting address of that memory space. - 4. When the memory space is not required, release
it back to the system for other functions.
15Computer Programming and Basic Software
Engineering
new and delete
6. Pointers and Arrays
The keywords new and delete help us claim and
release memory in Free Store.
Claim a piece of memory in Free Store with size
that is equal to an unsigned short integer.
unsigned short int pPointer pPointer new
unsigned short int // after using the memory
space delete pPointer // return it to system
We claim memory with size equals to 2
integers. pPointer2 now points to the starting
address of this memory space.
int pPointer2 pPointer2 new int2 //
after using the memory space delete
pPointer2 // return it to system
Results unpredictable is no
16Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
int pPointer unsigned short int
pPointer2 pPointer new int pPointer2 new
unsigned short int 2 delete pPointer //
return it to system delete pPointer2 //
return it to system
Free Store
Memory
Address
8003
8004
8005
8006
8007
8008
8009
800A
800B
800C
17Computer Programming and Basic Software
Engineering
Exercise 6.1
6. Pointers and Arrays
The program on the next page will introduce the
problem of memory leaks (the system cannot get
back the memory allocated to the program) and
execution error. Build the program and step over
each line of code using the Run-time Debugger.
Answer the following questions
- What is the address of localVariable?
- What is the value of pHeap after executing line
6? - What is the value of pHeap after executing line
11? - Assume that you can finish executing the program.
Do you think you can free the memory claimed by
the new statement in line 6? If no, why not?
Modify the program such that we can free the
memories claimed by both new statements in line 6
and line 11.
18Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
include ltiostreamgt using namespace std int
main() int localVariable 5 int pLocal
localVariable int pHeap new int //line
6 pHeap 7 cout ltlt "localVariable " ltlt
localVariable ltlt "\n" cout ltlt "pLocal " ltlt
pLocal ltlt "\n" cout ltlt "pHeap " ltlt pHeap ltlt
"\n" pHeap new int //line 11 pHeap
9 cout ltlt "pHeap " ltlt pHeap ltlt "\n" delete
pHeap delete pHeap return 0
19Computer Programming and Basic Software
Engineering
Stray (Wild or Dangling) Pointers
6. Pointers and Arrays
- When one deletes a pointer, the associated memory
will be given back to system. - If one tries to use that pointer again without
reassigning it, the result is unpredictable. - To ensure one will not use the deleted pointer
again, always assign the pointer with the value 0
after delete. - A stray (or wild, dangling) pointer is the
pointer that has been deleted but without
assigning to null.
delete a pointer ? remove a pointer, it still
exists
NULL points to ROM
int pNum new int(5) // Initialize pNum to
5 delete pNum pNum 0 // To ensure the
program will crash rather // than
unpredictable if one reuses it
20Computer Programming and Basic Software
Engineering
Creating Objects in the Free Store
6. Pointers and Arrays
- Similar to integer, we can create objects in the
Free Store.
Free Store or the heap
Size enough for a Cat
Cat pCat new Cat
pCat
- pCat stores the beginning address of the memory
allocated.
- When the object is created, its constructor is
called.
The Stack
Code Space
Object identified by a pointer.
Global Name Space
21Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Deleting Objects
- Objects in the Free Store can also be deleted.
Cat pCat new Cat delete pCat
- pCat becomes an identifier of the object created.
- When an object is deleted, its destructor will be
called. - Hence the destructor of Cat will be called when
the keyword delete is used in the above. - (The destructor will also be called if the
function that creates the object finishes.)
22Computer Programming and Basic Software
Engineering
6. Pointers References and Arrays
include ltiostreamgt using namespace std class
SimpleCat public SimpleCat()
SimpleCat() int GetAge() const return
itsAge void SetAge(int age) itsAge
age private int itsAge SimpleCatSimple
Cat() cout ltlt "Constructor called.\n"
itsAge 1 SimpleCatSimpleCat() cout ltlt
"Destructor called.\n"
Example
The class SimpleCat
Constructor
Destructor
23Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
int main() cout ltlt "SimpleCat
Frisky...\n" SimpleCat Frisky cout ltlt
"SimpleCat pRags new SimpleCat...\n" SimpleCa
t pRags new SimpleCat cout ltlt "delete
pRags...\n" delete pRags cout ltlt "Exiting,
watch Frisky go...\n" return 0
pRags in the stack, pRags in the heap
Output of the program
24Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Accessing Members of Objects
- To access members of an object, the symbol (.) is
used.
The Same
SimpleCat pCat new SimpleCat (pCat).SetAge(2)
The object pointed by pCat
The member function of the object
Input parameter of the member function
- In C, a shorthand is provided for such member
access
SimpleCat pCat new SimpleCat pCat-gtSetAge(2)
// The same as before
25Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
include ltiostreamgt using namespace std class
Object public Object() Object()
int GetCnt() const return
count private int count ObjectObject
() count new int(1) // initialize count
to 1 ObjectObject() delete count int
main() Object Obj return 0
Question
If I declare an object in the stack that has
member variables in the heap, what is in the
stack and what is in the heap?
26Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Answer
Free Store or the heap
4 bytes on the stack to hold Obj which contains a
pointer count. 4 bytes on the heap that is
pointed by count of Obj.
4 bytes count
Obj
The Stack
4 bytes count
Code Space
Global Name Space
27Computer Programming and Basic Software
Engineering
6. Pointers and Arrays
Pointers Arithmetic
- Pointers can be added or subtracted from one
another if they are of the same type.
Variables short int a, b
Memory
10
0A
21
3A
51
44
20
Address
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
cout ltlt "a " ltlt a ltlt endl // Assume a
0000 b a 1 cout ltlt "b " ltlt b ltlt endl //
b 0002 cout ltlt "b - a " ltlt b-a ltlt endl // b
- a 1
28Computer Programming and Basic Software
Engineering
Pointers Arithmetic
6. Pointers and Arrays
You should not DIRECTLY assign a value to a
pointer, e.g. int p0x00110110
- The same applies to objects.
Variables Cat a new Cat //Assume Cat takes
6 bytes
Memory
Address
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
Cat a new Cat Cat b cout ltlt "a " ltlt a ltlt
endl // Assume a 0000 b a 1 cout ltlt "b
" ltlt b ltlt endl // b 0006 cout ltlt "b - a "
ltlt b-a ltlt endl // b - a 1
29Computer Programming and Basic Software
Engineering
Exercise 6.1b
6. Pointers and Arrays
Find out the errors in the following programs.
Note the error messages when you build these
program. Fix the errors and rebuild it to verify
your corrections.
include ltiostreamgt using namespace std int
main() int pInt pInt 9 cout ltlt
"The value at pInt " ltlt pInt ltlt endl
return 0
include ltiostreamgt using namespace std int
main() int SomeVariable 5 cout ltlt
"SomeVariable " ltlt SomeVariable ltlt "\n"
int pVar SomeVariable pVar 9 cout
ltlt "SomeVariable " ltlt pVar ltlt "\n"
return 0
30Computer Programming and Basic Software
Engineering
Exercise 6.1c
6. Pointers and Arrays
Modify the program you wrote in exercises 5.2 a
and b such that a. The program will ask the user
if he wants to create the object Felix. If yes,
the object is created in the heap. If no, just
quit. b. As before, initialize the age and weight
of Felix to 5 and 10 using the constructor.
Display the age and weight of Felix. c. Ask the
user to enter the age and weight of Felix and
display them again. d. After printing the age
and weight of Felix, the program will repeatedly
ask the user whether he wants to (i) enter the
age and weight again (ii) destroy Felix and
create again or (iii) quit the program. Your
program should be able to perform the task the
user selected. e. Whenever Felix is destroyed,
print the current age and weight of Felix using
the destructor. f. Comment your program
appropriately.
31Acknowledgments
- The slides are based on the set developed by Dr.
Frank Leung (EIE).