From 35559dd585389a54a404cad228b73bb72d6dded1 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 16 Jan 2024 16:26:11 -0500 Subject: [PATCH] renaming two lectures --- lectures/04_pointers/README.md | 212 -------------------------- lectures/04_pointers/pointer_init.png | Bin 13847 -> 0 bytes lectures/05_classes_II/README.md | 101 ------------ 3 files changed, 313 deletions(-) delete mode 100644 lectures/04_pointers/README.md delete mode 100644 lectures/04_pointers/pointer_init.png delete mode 100644 lectures/05_classes_II/README.md diff --git a/lectures/04_pointers/README.md b/lectures/04_pointers/README.md deleted file mode 100644 index bda9d31..0000000 --- a/lectures/04_pointers/README.md +++ /dev/null @@ -1,212 +0,0 @@ -# Lecture 4 --- Pointers, Arrays, & Pointer Arithmetic - -- Pointers store memory addresses. -- They can be used to access the values stored at their stored memory address. -- They can be incremented, decremented, added, and subtracted. -- Dynamic memory is accessed through pointers. -- Pointers are also the primitive mechanism underlying vector iterators, which we have used with std::sort and -will use more extensively throughout the semester. - -## 4.1 Pointer Example - -- Consider the following code segment: - -```cpp -float x = 15.5; -float *p; /* equiv: float* p; or float * p; */ -p = &x; -*p = 72; -if ( x > 20 ) -cout << "Bigger\n"; -else -cout << "Smaller\n"; -``` - -The output is Bigger -because x == 72.0. What’s going on? - -![alt text](pointer_init.png "pointer_init") - -## 4.2 Pointer Variables and Memory Access - -- x is an ordinary float, but p is a pointer that can hold the memory address of a float variable. The difference -is explained in the picture above. -- Every variable is attached to a location in memory. This is where the value of that variable is stored. Hence, -we draw a picture with the variable name next to a box that represents the memory location. -- Each memory location also has an address, which is itself just an index into the giant array that is the computer -memory. -- The value stored in a pointer variable is an address in memory. The statement p = &x; takes the address -of x’s memory location and stores it (the address) in the memory location associated with p. -- Since the value of this address is much less important than the fact that the address is x’s memory location, -we depict the address with an arrow. -- The statement: *p = 72; causes the computer to get the memory location stored at p, then go to that -memory location, and store 72 there. This writes the 72 in x’s location. - -Note: *p is an l-value in the above expression. - -- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/pointers/example1/index.html). - -## 4.3 Defining Pointer Variables - -- In the example below, p, s and t are all pointer variables (pointers, for short), but q is NOT. You need the * -before each variable name. -```cpp -int * p, q; -float *s, *t; -``` -- There is no initialization of pointer variables in this two-line sequence, so the statement below is dangerous, -and may cause your program to crash! (It won’t crash if the uninitialized value happens to be a legal address.) -```cpp -*p = 15; -``` - -- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/pointers/example2/index.html). - -## 4.4 Operations on Pointers - -- The unary (single argument/operand) operator * in the expression *p is the “dereferencing operator”. It means -“follow the pointer”. *p can be either an l-value or an r-value, depending on which side of the = it appears on. -- The unary operator & in the expression &x means “take the memory address of.” -- Pointers can be assigned. This just copies memory addresses as though they were values (which they are). -Let’s work through the example below (and draw a picture!). What are the values of x and y at the end? -```cpp -float x=5, y=9; -float *p = &x, *q = &y; -*p = 17.0; -*q = *p; -q = p; -*q = 13.0; -``` -- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/pointers/example3/index.html). - -- Assignments of integers or floats to pointers and assignments mixing pointers of different types are illegal. -Continuing with the above example: -```cpp -int *r; -r = q; // Illegal: different pointer types; -p = 35.1; // Illegal: float assigned to a pointer -``` -- Comparisons between pointers of the form if ( p == q ) or if ( p != q ) are legal and very -useful! Less than and greater than comparisons are also allowed. These are useful only when the pointers are -to locations within an array. - -## 4.5 Exercise - -- Draw a picture for the following code sequence. What is the output to the screen? - -```cpp -int x = 10, y = 15; -int *a = &x; -cout << x << " " << y << endl; -int *b = &y; -*a = x * *b; -cout << x << " " << y << endl; -int *c = b; -*c = 25; -cout << x << " " << y << endl; -``` - -- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/pointers/example4/index.html). - -## 4.6 Null Pointers - -- Like the int type, pointers are not default initialized. We should assume it’s a garbage value, leftover from -the previous user of that memory. -- Pointers that don’t (yet) point anywhere useful are often explicitly assigned to NULL. - - NULL is equal to the integer 0, which is a legal pointer value (you can store NULL in a pointer variable). - - But NULL is not a valid memory location you are allowed to read or write. If you try to dereference or -follow a NULL pointer, your program will immediately crash. You may see a segmentation fault, a bus -error, or something about a null pointer dereference. - - NOTE: In C++11, we are encouraged to switch to use **nullptr** instead of NULL or 0, to avoid some -subtle situations where NULL is incorrectly seen as an int type instead of a pointer. For this course we -will assume NULL and nullptr are equivalent. - - We indicate a NULL or nullptr value in diagrams with a slash through the memory location box. - - Comparing a pointer to NULL is very useful. It can be used to indicate whether or not a pointer variable is -pointing at a useable memory location. For example, -```cpp -if ( p != NULL ) -cout << *p << endl. -``` -tests to see if p is pointing somewhere that appears to be useful before accessing and printing the value stored -at that location. -- But don’t make the mistake of assuming pointers are automatically initialized to NULL. - -## 4.7 Arrays - -- Here’s a quick example to remind you about how to use an array: -```cpp -const int n = 10; -double a[n]; -int i; -for ( i=0; i -- Remember: the size of array a is fixed at compile time. STL vectors act like arrays, but they can grow and -shrink dynamically in response to the demands of the application. - -- play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/pointers/example_arrays_1/index.html). - -## 4.8 Stepping through Arrays with Pointers (Array Iterators) - -- The array code above that uses [] subscripting, can be equivalently rewritten to use pointers. We can re-write it in two ways: - -### 4.8.1 First Approach - -```cpp -const int n = 10; -double a[n]; -double *p; -for ( p=a; p_}}~PyZ7Ch zdQMd2+Z76msQ0hyox_B?;9I4IlAN0O>+9><+8PK10s?_-Y;2pGo7B|Q!^6XG@297y znVFg4;NTn_9O~-oFfcIg@9&e7lYM-Aq@<*Tg@sjBRnN}Oy1KfIjEwg8_X!9H^78Tq z1_rFGtV&BumzI|3>FGm4LYkYKnbO}0LqU;1NsE0}^Llrzk5E`ohu)N3urf4q^YWV8 z38z<&F^r*ia@OUkD!f7P)mTexm`oBG9l9TiRrp^D;X-7dkiDbvlQS%S-;}jes^XrV zpgrxTks;MWC0FFuC~#-6JVb&4DD;rBF5aw4M2fU3Hhr~j;B_K0hO0Z?0DrBYb>n-# zZq3J>FskkKXZp{_Hi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iS<&%ZBVrk!dy(j3U~FJb%0A^X zZ+MOcswxqZIfq578UH1=b+m4>UN+xdN25s38Im -- Test 1 will be held **Thursday, 09/21/2023 from 6-7:50pm**. - – No make-ups will be given except for pre-approved absence or illness, and a written excuse from the Dean -of Students or the Student Experience office or the RPI Health Center will be required. - – If you have a letter from Disability Services for Students and you have not already emailed it to -ds_instructors@cs.rpi.edu, please do so ASAP. Shianne Hulbert will be in contact with you about -your accommodations for the test. -- Coverage: Lectures 1-6, Labs 1-3, and Homeworks 1-2. - -- OPTIONAL: you are allowed two physical pieces of 8.5x11” paper, that’s four “sides”. We will not collect these electronically and we will not pre-print them, you will have to bring these notes pages yourself if you want them. We will check at the start of the exam that you do not have more than two pieces of paper for your notes! - -- Bring to the exam room: - – Your Rensselaer photo ID card. - – Pencil(s) & eraser (pens are ok, but not recommended). The exam will involve handwriting code on paper (and other short answer problem solving). Neat legible handwriting is appreciated. We will be somewhat forgiving to minor syntax errors – it will be graded by humans not computers. - – Computers, cell-phones, smart watches, calculators, music players, etc. are not permitted. Please do not bring your laptop, books, backpack, etc. to the exam room – leave everything in your dorm room. Unless you are coming directly from another class or sports/club meeting. - - -# Lecture 5 --- Classes II: Sort, Non-member Operators - -- Classes in C++; -- Non-member operators - -## 5.1 C++ Classes - -- Nuances to remember - - - Within class scope (within the code of a member function) member variables and member functions of -that class may be accessed without providing the name of the class object. - - Within a member function, when an object of the same class type has been passed as an argument, direct -access to the private member variables of that object is allowed (using the ’.’ notation). - -## 5.2 Operator Overloading - -- When sorting objects of a custom class, we can provide a third argument to the sort function, and this third argument is a comparison function. -- What if we do not want to provide this third argument? The answer is: define a function that creates a < operator for objects of that class! At first, this seems a bit weird, but it is extremely useful. -- Let’s start with syntax. The expressions a < b and x + y are really function calls! -Syntactically, they are equivalent to operator< (a, b) and operator+ (x, y) respectively. -- When we want to write our own operators, we write them as functions with these weird names. -- For example, if we write: - -```cpp -bool operator< (const Date& a, const Date& b) { -return (a.getYear() < b.getYear() || -(a.getYear() == b.getYear() && a.getMonth() < b.getMonth()) || -(a.getYear() == b.getYear() && a.getMonth() == b.getMonth() && a.getDay() < b.getDay())); -} -``` -then the statement - -```cpp -sort(dates.begin(), dates.end()); -``` -will sort Date objects into chronological order. -- Really, the only weird thing about operators is their syntax. -- We will have many opportunities to write operators throughout this course. Sometimes these will be made class member functions, but more on this in a later lecture. - -## 5.3 Questions - -- Can you solve leetcode problem 905 with an overloaded operator <, and make this overloaded operator < a non-member function? -- Can you solve leetcode problem 905 with an overloaded operator <, and make this overloaded operator < a member function? -- Can you solve leetcode problem 905 with an overloaded operator <, and make this overloaded operator < a member function, plus make the definition of this member function outside of the class definition? - -## 5.4 Copy Constructor - -- A copy constructor is a constructor which is used to create a new object as a copy of an existing object of the same class. -- Copy constructors are automatically generated by the compiler if you do not provide one explicitly. However, if your class uses dynamic memory (which will be covered in next lecture), and you want a copy constructor, then you must write your own copy constructor. -- Copy constructors get called when you create a new object by copying an existing object using the assignment operator (=), or when you pass an object by value to a function. -- Still use the *Date* class as an example, if you have defined your own copy constructor whose prototype is like: - -```cpp -Date(const Date &other); -``` - -and when you have the following lines of code: - -```cpp -Date a; -Date b = a; -``` - -The first statement will call the default constructor, while the second statement will call the copy constructor. - -## 5.5 Exercises - -- [Leetcode problem 56: Merge Intervals](https://leetcode.com/problems/merge-intervals/). Solution: [p56_mergeintervals.cpp](../../leetcode/p56_mergeintervals.cpp) -- [Leetcode problem 905: Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/). Solution: [p905_sortarraybyparity.cpp](../../leetcode/p905_sortarraybyparity.cpp) -- [Leetcode problem 1929: Concatenation of Array -](https://leetcode.com/problems/concatenation-of-array/). Solution: [p1929_concatenationofarray.cpp](../../leetcode/p1929_concatenationofarray.cpp)