lecture_materials:week_00
This is an old revision of the document!
Table of Contents
Week 00 Materials
This week is for participants in the experimental CS 19 Boot Camp.
Slides
Monday
Forthcoming
Tuesday
scopes.cpp (illustrating locally and globally scoped variables)
- scopes.cpp
- /**
- * @file scopes.cpp
- * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu
- *
- * Let's see how much you understand/remember about scope and lifetime in C++!
- */
- #include <iostream>
- int x;
- void f1() {
- x = 2;
- std::cout << x << '\n';
- }
- void f2(int x) {
- std::cout << x << '\n';
- {
- int x;
- x = 4;
- std::cout << x << '\n';
- }
- x = 5;
- std::cout << x << '\n';
- }
- int main() {
- x = 1;
- std::cout << x << '\n';
- f1();
- std::cout << x << '\n';
- f2(3);
- std::cout << x << '\n';
- int x;
- x = 6;
- std::cout << x << '\n';
- }
Notes
Your job: Be the compiler and the OS and determine the output without using any tools but your mind!
Then compile and run this code to see if you were right.
scopes_annotated.cpp (illustrating locally and globally scoped variables)
- scopes_annotated.cpp
- /**
- * @file scopes_annotated.cpp
- * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu
- *
- * Let's add *memory addresses* to the output to see which symbol `x` represents in which context.
- */
- #include <iostream>
- int x; // global variable, global scope
- void f1() {
- x = 2; // assigns to global variable `x`
- std::cout << &x << '\t' << x << '\n';
- }
- void f2(int x) {
- std::cout << &x << '\t' << x << '\n';
- {
- int x; // local variable (local to function `f1()`), scoped within anonymous block
- x = 4; // assigns to local variable `x` scoped within anonymous block
- std::cout << &x << '\t' << x << '\n';
- }
- x = 5; // assigns to local variable `x` (function parameter)
- std::cout << &x << '\t' << x << '\n';
- }
- int main() {
- x = 1; // assigns to global variable (i.e., global var `x` is a *lvalue*)
- std::cout << &x << '\t' << x << '\n';
- f1();
- std::cout << &x << '\t' << x << '\n'; // function `f1()` should have mutated global variable `x`
- f2(3);
- std::cout << &x << '\t' << x << '\n'; // function `f2()` has not mutated global variable `x`
- int x; // local variable, local scope
- x = 6; // assigns to local variable `x` (global `x` is now "shadowed" for the rest of the block)
- std::cout << &x << '\t' << x << '\n';
- }
Notes
This is the same program, but prints the memory address of each variable in question. Note which x
variables are at the same address, i.e. are literally the same integer object.
raii.cpp (illustrating the concept of RAII)
- scopes_annotated.cpp
- /**
- * @file raii.cpp
- * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu
- *
- * RAII: "Resource acquisition is initialization"
- * Perhaps easier to understand: "scope-based resource management"
- *
- * An object's resources (memory storage, file handles, etc.) are allocated during initialization,
- * and released during destruction. When an object's lifetime ends, its resources should be
- * deallocated/cleaned up/closed/etc.
- *
- * We'll run this through `strace` to verify when files are opened/closed:
- * strace --trace=openat,close ./a.out </srv/datasets/shakespeare-othello.txt
- */
- #include <cctype>
- #include <fstream>
- #include <iostream>
- // Counts and returns the number of English vowel characters in an input stream.
- size_t count_vowels(std::istream &source) {
- size_t vowel_count = 0;
- for (char c; source >> c;) {
- c = std::tolower(c);
- if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
- ++vowel_count;
- }
- return vowel_count;
- }
- int main() {
- std::ifstream hamlet{"/srv/datasets/shakespeare-hamlet.txt"};
- // Preview of subtype polymorphism: A `std::ifstream` *is a* `std::istream`
- // (i.e., an input stream reading from a file is a specific kind of input stream)
- std::cout << "Hamlet: " << count_vowels(hamlet) << '\n';
- std::cout << "Hamlet again: " << count_vowels(hamlet) << '\n';
- {
- std::ifstream macbeth{"/srv/datasets/shakespeare-macbeth.txt"};
- std::cout << "Macbeth: " << count_vowels(macbeth) << '\n';
- // Lifetime of `macbeth` ends here. File will be closed.
- }
- std::ifstream othello{"/srv/datasets/shakespeare-othello.txt"};
- std::cout << "Othello: " << count_vowels(othello) << '\n';
- // std::cout is just a plain input stream:
- std::cout << "std::cin: " << count_vowels(std::cin) << '\n';
- // Lifetimes of `hamlet` and `othello` end here.
- // Will be closed in the opposite order of initialization (`othello`, then `hamlet`)
- }
Notes
RAII is a horrible acronym, but understanding its consequences in C++ is important.
Wednesday
TBD
Thursday
TBD
Friday
TBD
lecture_materials/week_00.1755566857.txt.gz · Last modified: by Jeffrey Bergamini