User Tools

Site Tools


lecture_materials:week_00

This is an old revision of the document!


Week 00 Materials

This week is for participants in the experimental CS 19 Boot Camp.

Monday

Tuesday

Scope and Lifetime

scopes.cpp (illustrating locally and globally scoped variables)

scopes.cpp
  1. /**
  2.  * @file scopes.cpp
  3.  * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu
  4.  *
  5.  * Let's see how much you understand/remember about scope and lifetime in C++!
  6.  */
  7.  
  8. #include <iostream>
  9.  
  10. int x;
  11.  
  12. void f1() {
  13. x = 2;
  14. std::cout << x << '\n';
  15. }
  16.  
  17. void f2(int x) {
  18. std::cout << x << '\n';
  19. {
  20. int x;
  21. x = 4;
  22. std::cout << x << '\n';
  23. }
  24. x = 5;
  25. std::cout << x << '\n';
  26. }
  27.  
  28. int main() {
  29. x = 1;
  30. std::cout << x << '\n';
  31. f1();
  32. std::cout << x << '\n';
  33. f2(3);
  34. std::cout << x << '\n';
  35. int x;
  36. x = 6;
  37. std::cout << x << '\n';
  38. }
Notes

Your job: Be the compiler and the shell and determine the output of this program without using any tools other than 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
  1. /**
  2.  * @file scopes_annotated.cpp
  3.  * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu
  4.  *
  5.  * Let's add *memory addresses* to the output to see which symbol `x` represents in which context.
  6.  */
  7.  
  8. #include <iostream>
  9.  
  10. int x; // global variable, global scope
  11.  
  12. void f1() {
  13. x = 2; // assigns to global variable `x`
  14. std::cout << &x << '\t' << x << '\n';
  15. }
  16.  
  17. void f2(int x) {
  18. std::cout << &x << '\t' << x << '\n';
  19. {
  20. int x; // local variable (local to function `f1()`), scoped within anonymous block
  21. x = 4; // assigns to local variable `x` scoped within anonymous block
  22. std::cout << &x << '\t' << x << '\n';
  23. }
  24. x = 5; // assigns to local variable `x` (function parameter)
  25. std::cout << &x << '\t' << x << '\n';
  26. }
  27.  
  28. int main() {
  29. x = 1; // assigns to global variable (i.e., global var `x` is a *lvalue*)
  30. std::cout << &x << '\t' << x << '\n';
  31. f1();
  32. std::cout << &x << '\t' << x << '\n'; // function `f1()` should have mutated global variable `x`
  33. f2(3);
  34. std::cout << &x << '\t' << x << '\n'; // function `f2()` has not mutated global variable `x`
  35. int x; // local variable, local scope
  36. x = 6; // assigns to local variable `x` (global `x` is now "shadowed" for the rest of the block)
  37. std::cout << &x << '\t' << x << '\n';
  38. }
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
  1. /**
  2.  * @file raii.cpp
  3.  * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu
  4.  *
  5.  * RAII: "Resource acquisition is initialization"
  6.  * Perhaps easier to understand: "scope-based resource management"
  7.  *
  8.  * An object's resources (memory storage, file handles, etc.) are allocated during initialization,
  9.  * and released during destruction. When an object's lifetime ends, its resources should be
  10.  * deallocated/cleaned up/closed/etc.
  11.  *
  12.  * We'll run this through `strace` to verify when files are opened/closed:
  13.  * strace --trace=openat,close ./a.out </srv/datasets/shakespeare-othello.txt
  14.  */
  15.  
  16. #include <cctype>
  17. #include <fstream>
  18. #include <iostream>
  19.  
  20. // Counts and returns the number of English vowel characters in an input stream.
  21. size_t count_vowels(std::istream &source) {
  22. size_t vowel_count = 0;
  23. for (char c; source >> c;) {
  24. c = std::tolower(c);
  25. if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
  26. ++vowel_count;
  27. }
  28. return vowel_count;
  29. }
  30.  
  31. int main() {
  32. std::ifstream hamlet{"/srv/datasets/shakespeare-hamlet.txt"};
  33. // Preview of subtype polymorphism: A `std::ifstream` *is a* `std::istream`
  34. // (i.e., an input stream reading from a file is a specific kind of input stream)
  35. std::cout << "Hamlet: " << count_vowels(hamlet) << '\n';
  36. std::cout << "Hamlet again: " << count_vowels(hamlet) << '\n';
  37. {
  38. std::ifstream macbeth{"/srv/datasets/shakespeare-macbeth.txt"};
  39. std::cout << "Macbeth: " << count_vowels(macbeth) << '\n';
  40. // Lifetime of `macbeth` ends here. File will be closed.
  41. }
  42. std::ifstream othello{"/srv/datasets/shakespeare-othello.txt"};
  43. std::cout << "Othello: " << count_vowels(othello) << '\n';
  44. // std::cout is just a plain input stream:
  45. std::cout << "std::cin: " << count_vowels(std::cin) << '\n';
  46. // Lifetimes of `hamlet` and `othello` end here.
  47. // Will be closed in the opposite order of initialization (`othello`, then `hamlet`)
  48. }
Notes

RAII is a horrible acronym, but understanding its consequences in C++ is important.

scope_lifetime_practice.cpp (an opportunity to think and practice)

scope_lifetime_practice.cpp
  1. /**
  2.  * @file scope_lifetime_practice.cpp
  3.  * @author Jeffrey Bergamini for CS 19 Boot Camp, jeffrey.bergamini@cabrillo.edu
  4.  *
  5.  * Some lunchtime practice!
  6.  */
  7.  
  8. #include <cctype> // `std::isprint()`
  9. #include <fstream> // `std::ifstream`
  10. #include <iostream> // `std::cout`
  11.  
  12. int main(int argc, char **argv) {
  13. std::ifstream f1(argv[1]);
  14. std::ifstream f2(argv[2]);
  15. // TODO: Find all the printable characters unique present in one file and not the other.
  16. // Print each unique character and its ASCII number, one character per line, in ascending order.
  17. }
Notes

Having some data to test with is useful. I have many textual datasets available. If you have your program in a form that is executable in a terminal, the following commands should work for testing:

Command:

./a.out <(echo suitably) <(echo implausible)

Expected Output:

e	101
m	109
p	112
t	116
y	121

Command:

./a.out <(curl -s https://jeff.cis.cabrillo.edu/datasets/genius.txt) <(curl -s https://jeff.cis.cabrillo.edu/datasets/injust.txt)

Expected output:

'       39
,       44
-       45
.       46
J       74
M       77
j       106
k       107
v       118

Command:

./a.out <(curl -s https://jeff.cis.cabrillo.edu/datasets/shakespeare-hamlet.txt) <(curl -s https://jeff.cis.cabrillo.edu/datasets/shakespeare-macbeth.txt)

Expected Output:

"       34
#       35
$       36
%       37
*       42
+       43
/       47
0       48
4       52
5       53
6       54
7       55
8       56
9       57
<       60
=       61
>       62
@       64
J       74
U       85
X       88
Z       90
_       95
j       106
~       126

Wednesday

TBD

Thursday

TBD

Friday

TBD

lecture_materials/week_00.1755580024.txt.gz · Last modified: by Jeffrey Bergamini