Week 6 (CS 200)
Table of Contents
Memory, pointers, and dynamic arrays
1. Class schedule
- Tuesday: Memory, Pointers
- Thursday: Lab
2. Assignment links
| Assignment | Reading link | Canvas link |
|---|---|---|
| 📚 Intro: Pointers and memory | Read | Canvas quiz |
| 📚 Intro: Dynamic arrays | Read | Canvas quiz |
| 💾 Lab: Pointers | Read | Canvas assignment |
| 💬 Discussion: Design bias | Canvas discussion | |
| 🙆 Update: Week 06 | Canvas assignment |
3. Example code
We will go over these during class!
3.1. Pointers
3.1.1. Variable addresses and sizes
#include <iostream> #include <iomanip> #include <string> using namespace std; int main() { bool myBoolean = true; int myInteger = 1; float myFloat = 2.3; string myString = "Hello"; cout << left; cout << setw( 15 ) << "VAR" << setw( 10 ) << "VALUE" << setw( 20 ) << "ADDRESS" << setw( 5 ) << "SIZE" << endl << string( 80, '-' ) << endl; cout << setw( 15 ) << "myBoolean" << setw( 10 ) << myBoolean << setw( 20 ) << &myBoolean << setw( 5 ) << sizeof( myBoolean ) << endl; cout << setw( 15 ) << "myInteger" << setw( 10 ) << myInteger << setw( 20 ) << &myInteger << setw( 5 ) << sizeof( myInteger ) << endl; cout << setw( 15 ) << "myFloat" << setw( 10 ) << myFloat << setw( 20 ) << &myFloat << setw( 5 ) << sizeof( myFloat ) << endl; cout << setw( 15 ) << "myString" << setw( 10 ) << myString << setw( 20 ) << &myString << setw( 5 ) << sizeof( myString ) << endl; return 0; }
3.1.2. Pointers to existing variables
#include <iostream> #include <string> using namespace std; int main() { string studentA = "Hikaru"; string studentB = "Umi"; string studentC = "Fuu"; string* ptrTopStudent = nullptr; // Declare pointer bool done = false; while ( !done ) { cout << "Students:" << endl << "A. " << studentA << "\t" << &studentA << endl << "B. " << studentB << "\t" << &studentB << endl << "C. " << studentC << "\t" << &studentC << endl << endl; cout << "Top student address: " << ptrTopStudent << endl; if ( ptrTopStudent != nullptr ) { // De-reference pointer cout << "Top student name: " << *ptrTopStudent << endl; } cout << endl << "Set top student to who? (A, B, C, or Q to quit): "; char choice; cin >> choice; switch( choice ) { // Assign pointer to address of variable case 'A': ptrTopStudent = &studentA; break; case 'B': ptrTopStudent = &studentB; break; case 'C': ptrTopStudent = &studentC; break; case 'Q': done = true; break; default: cout << "Invalid selection" << endl; } } return 0; }
3.1.3. Pointers and objects
#include <iostream> #include <string> using namespace std; struct Student { string name; int grade; }; int main() { const int STUDENT_COUNT = 2; Student students[STUDENT_COUNT]; Student* ptrCurrent; for ( int i = 0; i < STUDENT_COUNT; i++ ) { ptrCurrent = &students[i]; cout << "Enter a name: "; cin >> ptrCurrent->name; cout << "Enter the grade: "; cin >> ptrCurrent->grade; } for ( int i = 0; i < STUDENT_COUNT; i++ ) { ptrCurrent = &students[i]; cout << "NAME: " << ptrCurrent->name << endl; cout << "GRADE: " << ptrCurrent->grade << endl; } return 0; }
3.2. Dynamic memory
3.2.1. Single variable
#include <iostream> #include <string> using namespace std; int main() { int* myInteger; myInteger = new int; (*myInteger) = 30; cout << "Address of new integer: " << myInteger << endl; cout << "Value of new integer: " << (*myInteger) << endl; delete myInteger; // Free the memory myInteger = NULL; // Reset the pointer to no address return 0; }
3.2.2. Array
#include <iostream> #include <string> using namespace std; int main() { // First array string* names = new string[ 5 ]; names[0] = "Snake"; names[1] = "Ocelot"; names[2] = "Octopus"; names[3] = "Fox"; names[4] = "Wolf"; // Oops, need more space... // Create a temporary array with a bigger size: string* newArray = new string[ 10 ]; // Copy over the contents of the old array: for ( int i = 0; i < 5; i++ ) { newArray[i] = names[i]; } // Delete the old array, free that memory: delete [] names; // But, we still want to use the original array name. // So, we will change pointers! // Point "names" to the new, bigger array, // and reset our temporary array to NULL. names = newArray; newArray = NULL; // Now we can keep using the original names array names[5] = "Raven"; names[6] = "Mantis"; names[7] = "Fortune"; names[8] = "Vamp"; names[9] = "Fatman"; // Output the results! :) for ( int i = 0; i < 10; i++ ) { cout << (i+1) << ": " << names[i] << endl; } // Don't forget that we need to delete // the bigger array once the program ends. // names is currently pointing to the big array. delete [] names; return 0; }