Title: COT 3002 Foundations of Computer Science
1COT 3002 - Foundations of Computer Science
Pointers and Dynamically Allocated Memory
- Professor Georgiana Hamza-Lup
- ghamzal_at_fau.edu
2Pointers
- A pointer is the memory address of a variable
- A pointer "points" to a variable by telling where
that variable is located - The memory address of a variable can be used
(similarly to its name) to access that variable - If a variable is stored on four bytes, the
address of the first can be used to access that
variable - When a variable is used as a call-by-reference
argument, its address is passed
3Declaring Pointer Variables
- A pointer can be stored in a variable
- Pointer variables must be declared to have a
pointer type - double p
- To declare multiple pointers in a statement, use
the asterisk before each pointer variable - int p1, p2, v1, v2
declares a pointer variable p that can "point" to
a variable of type double (the asterisk
identifies p as a pointer variable)
p1 and p2 point to variables of type intv1 and
v2 are variables of type int
4The and Operators
- The operator (the address of operator) can be
used to determine the address of a variable
(which can be assigned to a pointer variable)
int v1 10 int p1 p1 v1
- p1 is now a pointer to v1
- v1 can be called " the variable pointed to by p1"
- The operator (the dereferencing operator) can
be used to access the variable pointed to by a
pointer
int v1 0 int p1 v1 p1 42 cout ltlt v1
ltlt p1
v1 and p1 now refer to the same variable p1 is
said to be dereferenced
42 42
Output
5Pointer Assignment
6Dynamic Variables
- Variables created using the new operator are
called dynamic variables - Dynamic variables can be created and destroyed at
any time, while the program is running - An area of memory called the freestore
isreserved for dynamic variables - New dynamic variables use the freestore
- If all of the freestore is used, calls to new
will fail - Un-needed memory can be recycled
- When dynamic variables are no longer needed, they
can be deleted, using the delete operator, and
the memory they used is returned to the freestore
7The new Operator
- The new operator allocates space for a variable
and returns a pointer to it (its address) - That pointer is the only way to access that
variable (that variable does not have a name) - new is followed by the size of the memory to be
allocated for that variable, which is expressed
in terms of a data type
space for a double variable is allocated and the
pointer variable p1 points to that variable (the
new variable is referred to as p1)
double p1 new double
space for 4 int variables is allocated and the
pointer variable scores points to the first
variable
int scores new int4
Dynamically allocated array
8Example
9The delete Operator
- The delete operator can be used to destroy
dynamic variables (created using the new
operator) - delete is followed by the name of the pointer
variable that points to dynamic variable
int p1 new int delete p1
the value of p1 is now undefined and the memory
used by the variable that p1 pointed to is back
in the freestore
- Dangling pointers
- Undefined pointer variables are called dangling
pointers - Dereferencing a dangling pointer (p) is usually
disastrous
10Automatic vs. Dynamic variables
- Variables declared in a function are created
automatically when the function is called and
destroyed automatically when the function ends - Variables declared outside any function
definition (global variables) are created
automatically when the program begins and
destroyed automatically when the program ends - The above two types of variables are called
automatic variables because their creation and
destruction are controlled automatically - Variable created with the new operator are called
dynamic variables - The programmer manually controls creation and
destruction of such variables with operators new
and delete
11Type Definitions
- A name can be assigned to a type definition, and
then used to declare variables - The keyword typedef is used to define new type
names - Syntax typedef known_type_definition
new_type_name - to avoid mistakes using pointers, define a new
type name IntPtr as follows - typedef int IntPtr
-
- IntPtr can now be used to declare pointers to
int variables - IntPtr p ? int p
12Type Definitions Usage
- typedef int IntPtr
- Prevent errors in pointer declaration int
p1, p2 // only p1 is a pointer variable - IntPtr p1, p2 // p1 and p2 are pointer
variables - A second advantage in using typedef is seen in
parameter lists - void sample_function (IntPtr pointer_var)
- is less confusing than
- void sample_function ( int pointer_var)
13Pass-by-value vs. pass-by-reference for pointer
variables
include ltiostreamgt using namespace std void
my_function(int p) p 50 int tmp new
int tmp 41 p tmp int main() int
p1 new int p1 40 my_function(p1) cout
ltlt p1 return 0
include ltiostreamgt using namespace std void
my_function(int p) p 50 int tmp new
int tmp 41 p tmp int main() int
p1 new int p1 40 my_function(p1) cout
ltlt p1 return 0
14(1) int p1 new int
include ltiostreamgt using namespace std void
my_function(int p) p 50 int tmp new
int tmp 41 p tmp int main() int
p1 new int p1 40 my_function(p1) cout
ltlt p1 return 0
(2) p1 40
(3) my_function(p1)
(4) p 50
(5) int tmp new int
(6) tmp 41
(7) p tmp
Output
50
15(1) int p1 new int
include ltiostreamgt using namespace std void
my_function(int p) p 50 int tmp new
int tmp 41 p tmp int main() int
p1 new int p1 40 my_function(p1) cout
ltlt p1 return 0
(2) p1 40
(3) my_function(p1)
(4) p 50
(5) int tmp new int
(6) tmp 41
(7) p tmp
Output
41
16Dynamic Arrays
- A dynamic array is an array whose size is
determined when the program is running, not when
you write the program - Static arrays require the programmer todecide
the size of the array when the program is written - What if the programmer estimates too large?
- Memory is wasted
- What if the programmer estimates too small?
- The program may not work in some situations
- Dynamic arrays can be created with just the
right size while the program is running
17Creating/Destroying Dynamic Arrays
- Dynamic arrays are created using the new operator
double d new double10 or int sizecin ltlt
size double d new doublesize
creates an array of 10 elements of type double
d can now be used as if it were an ordinary array
- When finished with the array, it should be
deleted to return memory to the freestore - delete d
- The brackets tell C that a dynamic array is
being deleted so it must check the size to know
how many indexed variables to remove - Forgetting the brackets, is not illegal, but
would tell the computer to remove only one
variable
18Pointer Variables as Array Variables
- Array variables are actually pointer variables
that point to the first element in the array - Since a is a pointer variable that points to
a0, - p a causes p to point to the same location as
a - Pointer variable p can be used as if it were an
array variable p0, p1, p9 are all legal
ways to use p - Variable a can be used as a pointer variable
except the pointer value in a cannot be changed
int a10 int p
Variables a and p are the same kind of variable
int p2 new int10 a p2 // attempt to
change a
19Static vs. Dynamic Arrays
- Static arrays their size is determined at
compile time - int sgrades4 or const int
NO_OF_STUDENTS 4 - int sgrades NO_OF_STUDENTS
- Dynamic arrays their size is determined at
run-time -
- int number
- cout ltlt "Enter number of students " cin gtgt
number int dgradesnumber - int dgrades new intnumber
20Static vs. Dynamic Arrays (cont.)
- The size of an array (static or dynamic) CANNOT
change during run-time - But, with dynamic arrays I can do the following
int main () int number 100 int
grades new intnumber //if
at any point in the program I need a
larger/smaller array then //1. create a
new array int temp new intnumber10
//2. copy the elements from the old array to
the new one for (int i0 ilt number i)
tempi gradesi //3. delete
the old array delete grades //4.
link the pointer to the new array grades
temp
21Pointer Arithmetic
- Arithmetic can be performed on the addresses
contained in pointers - recall that a points to a0
- The expression a1 evaluates to the address of
a1 and a2 evaluates to the address of a2 - Notice that adding one adds enough bytes for one
variable of the type stored in the array - You can use add and subtract with pointers
- The and - - operators can be used
- Two pointers of the same type can be subtracted
to obtain the number of indexed variables between
them (the pointers should be in the same array)
for (int i 0 i lt array_size i) cout ltlt (a
i) ltlt // same as cout ltlt ailtlt
22const with Pointers
int main () const int i 10 int b 100, c
55 b i i b //error const int p
c p b p 20 //error because p is
declared const. You are forbidden to
//use p to change any of the variables that p
points to. b 20 //but you can change those
variables through other means int q i
//error type incompatibility const int q
i q 50 i 50 //error int const r
c //notice that const appears after r b
//error because of const you are forbidden to
change r r 30 const int const t b