Professional Documents
Culture Documents
Review: Pointers
Pointers store memory addresses
Stack Heap
902204 21 7 1000
35
1000 7
1000
12
* dereferences a pointer
Review: new
Stack
int *px 1000
35
Heap
void foo(int *x) { cout << *x << endl; (*x)++; }; void foo(int &x) { cout << x << endl; x++; }
Sizes of datatypes
Every datatype has a size in bytes
char: 1 byte int: 4 bytes double: 8 bytes Vector/Grid/Queue/Map/Stack: many bytes (depends on number of elements) pointer: 4 or 8 bytes (depending on your operating system)
Remember how weve been telling you since week 1 to pass large containers by reference?
1) Saves the cost of copying a large container 2) Since references are just pointers, passing a reference only takes 4 or 8 bytes
Remember that pointers store memory addresses A 32 bit pointer can store 232 different memory address A 64 bit pointer can store 264 different memory address Pointer size determines how much memory (RAM) your computer can hold
NULL
Remember that C++ doesnt initialize primitives for you
int x; cout << x << endl;
int x
??????
If a pointer isnt initialized to a value, then the address it points to will be random NULL is a value used to communicate that a pointer doesnt point to anything
Stack
int *px NULL
Heap
NULL
What happens when you run the following code?
int *px; *px = 1;
int *px
??????
NULL
Orphaned Memory
Stack
int *px
Heap
Stack
int *px
Heap
35 px = new int;
????
Orphaned Memory
When a program orphans memory we say it leaks memory
delete
Stack
int *px
Heap
35
???? int *px; px = new int; *px = 35; delete px; px = new int;
delete
Stack
int *px
Heap
???? int *px; px = new int; *px = 35; delete px; px = new int;
delete returns the memory to the operating system, so it can be allocated again to future calls to new
delete
Stack
int *px int *px = new int; *px = 35; delete px;
Heap
35
Stack
int *px
Heap
21
Writing or reading after delete will work, but its not safe. Hopefully it will crash your program
delete
Stack
int *px
Heap
????
delete
Stack
int *px
Heap
21
delete
Stack
int *px
Heap
21
delete
Stack
int *px
Heap
int *py
21
delete
Stack
int *px
Heap
int *py
delete
Stack
int *px
Heap
int *py
21
delete
Stack
int *px
Heap
int *py
21
int *px = new int; *px = 35; delete px; py = new int; *py = 0; *px = 21; if (*py == 21) KillAKitten();
delete
Java: never need to call delete because Garbage Collector frees memory that is not being used. The lack of a Garbage Collector in C++ is one the reasons C++ is so much harder to program in, but it is one of the major reasons C++ is faster than Java
C++ Arrays
Arrays are a fixed size block of elements
int arr[10]; arr[9] arr[2]
arr[1] int arr[] arr[0]
Stack
??? ???
??? ???
Can use [] to access and modify elements Arrays do not do any bounds checking for you
arr[10] = 7; //Will compile, but you shouldnt do this
Cannot call methods on arrays, and arrays do not know how big they are
arr.size() //ERROR
C++ Arrays
Writing beyond the bounds of an Array it is called a buffer overflow
int bankDeposit = 100; int arr[10]; arr[10] = 0;
Stack
bankDeposit arr[9] 100 ???
C++ Arrays
Writing beyond the bounds of an Array it is called a buffer overflow
int bankDeposit = 100; int arr[10]; arr[10] = 0;
Stack
bankDeposit arr[9] 0 ???
C++ Arrays
Arrays are pointers that point to the first element in the array
Stack
int arr[10]; int *px = NULL; px = arr; px[1] = 5; arr[9] ???
C++ Arrays
Arrays are pointers that point to the first element in the array
Stack
int arr[10]; int *px = NULL; px = arr; px[1] = 5; arr[9] ???
C++ Arrays
Arrays are pointers that point to the first element in the array
Stack
int arr[10]; int *px = NULL; px = arr; px[1] = 5; arr[9] ???
C++ Arrays
Arrays are pointers that point to the first element in the array
Stack
int arr[10]; int *px = NULL; px = arr; px[1] = 5; arr[9] ???
??? 5 ???
C++ Arrays
The following are valid ways to create an array
const int ARRAY_SIZE = 100; int arr[10]; int otherArr[ARRAY_SIZE];
C++ Arrays
Dynamically allocate an array on the heap
int arrSize = GetInteger(); int *arr = new int[arrSize];
Stack
int *arr
???
arr[0]
Heap
???
arr[1]
???
arr[arrSize-1]
Stack
int *arr
Heap
???
arr[0]
???
arr[1]
???
arr[arrSize-1]
Dont access memory that has been deleted When accessing elements in an Array, check to make sure the index is in bounds
Stack
int **px
Heap
Stack
int **px
Heap
????
Stack
int **px
Heap
????
Stack
int **px
Heap
????
Stack
int **px
Heap
????
Stack
int **px
Heap
????
Stack
int **px
Heap
0 1
???
99
int *[100]
int **p
???
0
???
1
int[2]
???
0
???
1
???
2
int[3]
Stack
int *p NULL
Heap
Stack
int *p NULL
Heap
////////// NULL
Stack
int *p NULL ???
0
Heap
???
1
???
99
//////////
Stack
int *p NULL ???
0
Heap
???
1
???
99
Stack
int *p NULL
Heap
Stack
int *p NULL
Heap
//////////
Stack
int *p ???
Heap
???
1
???
99
//////////
Stack
int *p ???
0
Heap
???
1
???
99
The End