Memory
Memory Management
Memory management
Arrays
Dynamic memory
Dynamic arrays and pointers
Building a vector class
Memory Management
Memory Requirements
code
storage
data
storage
Memory Management
When
There
RAM
We
(-1)
RAM
RAM Illustrated
Consider a computer with 1GB * of RAM
*1 GB = 1,073,741,824 bytes
0
107374181107374181107374181107374181107374182107374182107374182107374182
6
7
8
9
0
1
2
3
Goals
As
efficiency
Low
overhead
Stack Memory
There
sequence
With no gaps
23.567
3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612
Another Example
int main(){
int r = 3;
double area =
circleArea(r);
double square(double
x){
return x * x;
}
main memory
x
3.141
3
3
3
5
r
radius
pi
square's
double circleArea(double
radius){
double pi = 3.1415;
double sq_r =
square(radius);
return sq_r * pi;
}
memory
Another Example
int main(){
int r = 3;
double area =
circleArea(r);
double square(double
x){
return x * x;
}
main memory
3.141
3
3
9
5
r
radius
pi
sq_r
r
radius
pi
double circleArea(double
radius){
double pi = 3.1415;
double sq_r =
square(radius);
return sq_r * pi;
}
sq_r
Another Example
int main(){
int r = 3;
double area =
circleArea(r);
double square(double
x){
return x * x;
}
main memory
28.27
3
4
r
area
r
area
double circleArea(double
radius){
double pi = 3.1415;
double sq_r =
square(radius);
return sq_r * pi;
}
Stack Memory
In
Reference Parameters
Some
And
Stack Problems
There
vid*
*very important
double
vid*
v*
n
i
*very important *v is a reference to vec, it
double
actually contains the byte
address of vec
Types Of Memory
It
These
Storage
code
storage
static
data
storage
dynami
c
automati
c
Static Storage
Statically
maintain them
Static
Automatic Storage
Function
Consider
Arrays
A Digression From Memory
Arrays
Before
What Is An Array?
An
The
Declaring Arrays
An
type of the
data stored in
the array
brackets declare
that the variable is
an array
Array Indexing
The
elements
The first element always has an index of 0
The last index is always array size 1
Array
Using an Array
double arr[4]; // array
arr[0] = 1.23; //assign
arr[3] = 2.14; //assign
double x = 0;
x = arr[3]; //access 4th
1.23
arr[0]
2.14
arr[1]
arr[2]
arr[3]
of 4 doubles
1st element
4th element
element
2.1
0
4
x
Initializing Arrays
Array
There
Array Size
An
An
loops
Allowing each element to be accessed in
turn
The loop control variable is used as an
index into the array
The loop ends once the array has been
processed
Often when the loop control variable is equal
to the size of the array
This
For Loops
// Assume an int array named arr,
// with a constant called ARR_SIZE
// The loop prints the contents of arr
for (int i = 0; i < ARR_SIZE; i++){
cout << arr[i] << endl;
}
The condition is i < ARR_SIZE
because the last legal index is
ARR_SIZE 1
Assumes the existence of a constant
called ARR_SIZE, e.g. const int
ARR_SIZE = 10;
While Loops
// Assume an int array named arr,
// with a constant called ARR_SIZE
// The loop prints the contents of arr
int i = 0;
while (i < ARR_SIZE){
cout << arr[i] << endl;
i++;
}
A very similar loop to the for loop,
we must not forget to increment
the index, i
Size as a Constant
It
Here
Sum Function
// Sums the array, returning the sum
int sumArray(int arr[], int sz){
int sum = 0;
for (int i = 0; i < sz; i++){
sum += arr[i];
}
return sum;
}
sz is used to specify the size of the
array
To use this function give it the
appropriate arguments
x = sumArrray(myArr, ARR_SIZE);
Searching Arrays
A
The
processed
e.g. summing or calculating the average of an
array
Consider
requires
Array, and its maximum size, or
Array, and current size ,or
Returning Arrays
Arrays
Changing Arrays
What
Array Parameters
int main()
{
double arr[] = {1, 2, 3};
printArray(arr, ARR_SIZE);
cout << "Running: void doubleArray";
cout << "(double arr[], int sz);" << endl;
doubleArray(arr, ARR_SIZE);
What has
printArray(arr, ARR_SIZE);
happened?
return 0;
The array passed
}
to the function
has changed.
This does not
happen with
other pass-byvalue
What's an Array?
To
The
arr[8] = 23;
To
index
Add this to the address in arr to find the
address of arr[8] element
Known as an offset calculation
an array is passed to a
function it is passed much like a
reference parameter
That is, the function is given the main
Pointer Basics
int
It is a type that called a pointer
Actually a pointer to an int
Variables Review
A
Main Memory
Main memory
is a sequence
of bytes
0
20
2220
-11
23
Pointers
A
value
They are called pointers as they can be
considered to point to a variable
It
store addresses
So
and
To ensure that the appropriate
operations are used with the data
Declaring a Pointer
Pointer
This
Declaring a Pointer
Previously
this
int * p;
The spaces are not necessary
You
can do this
int *p;
Or
this
int* p;
Or
even this
But this is kind of
int*p;
ugly!
int x = 12;
int *p = x;
Remember
Address Operator
Pointers
212
2201
23
int x =
23;
int* p =
&x;
0
212
409
6 address
the
p contains
of x
& is the address of
operator
23
2201
Dereferencing
Pointers
Pointer Assignment
int x = 12;
int *p = &x; //assign p the address of x
// Use p to assign 23 to x
*p = 23; //dereferences p
cout << x << endl;
cout << p << endl;
address
cout << *p << endl; value of
of x
x
value of the
variable that p
points to (i.e. x)
Cylinder
sizeof
Reference Operator
The
variable
Overloaded Operators
The * operator
multiplication: 12 * 3;
declaration of a pointer: int* p;
dereference: *p = 23;
y)
Pointers and
Assignment
int
int
int
int
x =
y =
*p1
*p2
12;
77;
= &x; //assign p1 the address of x
= &y; //assign p2 the address of y
p1
p2
1
2
7
7
x
y
Pointers and
Assignment
int x = 12;
int y = 77;
int *p1 = &x; //assign p1 the address of x
int *p2 = &y; //assign p2 the address of y
p1 = p2; //assigns the address in p2 to p1
p1
p2
1
2
7
7
x
y
Pointers and
Assignment
int
int
int
int
*p1
x = 12;
y = 77;
*p1 = &x; //assign p1 the address of x
*p2 = &y; //assign p2 the address of y
= *p2;
p1
p2
7
1
2
7
7
7
x
y
Dynamic Memory
time
But it has a duration and size that can
Dynamic Memory
Dynamic
Uses of Dynamic
Memory
There
It
sequentially
And all memory associated with a
function is released when the function
ends
Pointers
Data
memory
Pointers themselves are usually
Dynamic Arrays
And Back To Arrays
Dynamic Memory
Think
Stack
amount
be
to
allocated is easily
managed
Dynamic
memory,
reserved when needed at
run-time, but needs more
administration to use
Dynamic Arrays
int arrSize = 0;
cout << "How big an array? ";
cin >> arrSize;
int* arr = new int[arrSize];
The variable is a
pointer, used to
store an address
Creates this
array in dynamic
memory
Data created in dynamic
memory persists until the
application is terminated,
regardless
of
which
function it was created in
using an index
A
Deleting Arrays
Dynamic
memory continues to be
allocated even outside the scope in
which it was created
Whereas memory for a function's
until
The application terminates, or
It is explicitly released
delete
Every
Dynamic Arrays
int arrSize = 0;
cout << "How big an array? ";
cin >> arrSize;
int* arr = new int[arrSize];
// arr is used in the program
delete[] arr;
Deallocates the
dynamic
memory
Returning Arrays
An
But
it must be returned
as a pointer
int*
oddArray(int
sz){
int* result = new int[sz];
for (int i = 0; i < sz; i++){
result[i] = i * 2 + 1;
}
Use the function something
return result; like this:
}
int* arr = oddArray(10);
arr should be deleted at some
point
...)
To only reserve space for large objects
when necessary
To create reference structures
Memory
Memory
Dynamic
memory
heap
New
Pointers
pointers
Such
program is running
This allows, for example, vectors to be
created with the size determined at run-
The
numbers
classes
Some objects can be very large
Objects may contain bitmaps, or sound
files
Without dynamic memory space has to
be reserved for objects that may not be
used
Pointers
must be dereferenced to
access an object's methods
New Example
int *p1 = new int; //pointer to an int
// Make a new Student, default constructor
int *p2 = new Student(); ... assumes the
existence of a
// Make a new Student
Student class ...
int *p3 = new Student(94101);
*p1 = 119;
int id = p2.getID();
Error! p2 is not a
Student it is an address
...
This
It
Referring to Objects
The
The
variables
Use dot notation for such variables
Deletion
Stack
Deleting Memory
The
delete Operator
The
Each
Building a Simple
Vector Class
Bringing It All Toegether