Static Class Members and Pointers
Transcript of Static Class Members and Pointers
-
8/4/2019 Static Class Members and Pointers
1/17
Static Class Members
COMP 105 OOP&D
-
8/4/2019 Static Class Members and Pointers
2/17
Non Static Members
Rectangle 1
length
width
Rectangle 2
length
width
-
8/4/2019 Static Class Members and Pointers
3/17
Static MembersStudent 1
name
gpa
id
Student2
name
gpa
id
cautionFee
Static Member
Static member is
shared by all
objects of the class
-
8/4/2019 Static Class Members and Pointers
4/17
Pointers and Dynamic Memory
Allocation
COMP 105 Object Oriented
Programming & Design
-
8/4/2019 Static Class Members and Pointers
5/17
What is a pointer?
Pointer variables contain the address of other variables as
their values.
A pointer variable is declared using an asterisk (*) between
the data type and the variable.
E.g. int *p;declares p as a pointer to a memory space of type int
-
8/4/2019 Static Class Members and Pointers
6/17
Address of Operator
In C++ the address of operator (&) returns the address of its
operand.
E.g. int *p;
int num = 10;
p = # //sets the value of p to the address of num
p 10
num
-
8/4/2019 Static Class Members and Pointers
7/17
The Dereferencing Operator
When used as a unary operator, * is called thedereferencing operator.
It accesses the location pointed to by a
pointer. E.g. If p is a pointer to a location of type int,
then the statement *p = 25; sets the value of
the memory location pointed to by p to 25.
p 25
-
8/4/2019 Static Class Members and Pointers
8/17
Pointer Initialization
Pointers can be initialised to:
The integer 0
The constant NULL A pointer variable of the same type
-
8/4/2019 Static Class Members and Pointers
9/17
Pointer Arithmetic
Add /subtract an integer to/from a pointer
variable
The value added/subtracted is the integer times
the size of the object to which the pointer is
pointing
-
8/4/2019 Static Class Members and Pointers
10/17
Comparing and Assigning Pointers
Pointers may be compared using relational
operators.
The value of one pointer may be assigned to
another pointer of the same type.
-
8/4/2019 Static Class Members and Pointers
11/17
Comparing and Assigning Pointers
E.g. int x = 7;
int *p1, *p2;
p1 = &x;
p1 7
x
The statement q == p is now true. Why?
-
8/4/2019 Static Class Members and Pointers
12/17
Comparing and Assigning Pointers
E.g. int x = 7, y = 7;
int *p1, *p2;
p1 = &x;
p1 7
x
The statement q == p is not true. Why not?
p2 7
y
-
8/4/2019 Static Class Members and Pointers
13/17
Memory Leaks
Memory leak takes place when a memory
allocated from the free store is no longer
needed and is not released by delete operator.
One way this can occur is by reassigning a
pointer before a delete operator had a
opportunity to do its job:
-
8/4/2019 Static Class Members and Pointers
14/17
Memory Leaks
int * ptr;
ptr = new int;
*ptr = 31;
pPointer = new int;
*pPointer = 15;
ptr
-
8/4/2019 Static Class Members and Pointers
15/17
Memory Leaks
int * ptr;
ptr = new int;
*ptr = 31;
pPointer = new int;
*pPointer = 15;
ptr 31
-
8/4/2019 Static Class Members and Pointers
16/17
Memory Leaks
int * ptr;
ptr = new int;
*ptr = 31;
pPointer = new int;
*pPointer = 15;
ptr
31
15
-
8/4/2019 Static Class Members and Pointers
17/17
Memory Leaks
int * ptr;
ptr = new int;
*ptr = 31;
pPointer = new int;
*pPointer = 15;
ptr
31
15
The original memory
address is lost, and
therefore, it cannot be
released.