Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 333 – Section 5 C++ (1m).

Similar presentations


Presentation on theme: "CSE 333 – Section 5 C++ (1m)."— Presentation transcript:

1 CSE 333 – Section 5 C++ (1m)

2 Section Feedback Learn to throw More: Tools/Tricks/Tips
Candy/food Jokes Serious Spanish Tools/Tricks/Tips Whiteboard Diagrams are… Helpful Not helpful Yet this person copies one on the back of the index card… So, I took a look at all the feedback you guys gave and these are some of the ones given Here’s a joke I just heard yesterday: How many tickles does it take to tickle an octopus? Ten-tickles!

3 Overview C++ Classes, Constructors, new, delete, etc.
Drawing Memory Diagrams (1m)

4 C++ classes Encapsulation and Abstraction Access specifiers:
Public: anything outside the class can access it Protected: only this class and derived classes can access it Private: only this class can access it Polymorphism Multiple Inheritance (3m) Why do we want classes? Encapsulation and Abstraction There are 3 specifiers… C++ classes also support Polymorphism, which means that they support generic typing using templates Additionally, (you haven’t covered this but this is probably the scariest part of C++), C++ classes also support multiple inheritance (Oooooh! You should be scared by that! Can somebody tell me why that is scary??)

5 new and delete new is used to allocate objects and primitive data types on the heap delete is used to deallocate these heap allocated objects Use “delete [ ] array” on an array Unlike malloc() and free(), new and delete are operators (3m) PLEASE be sure to match up the appropriate deallocated with the corresponding allocator used. Behavior is undefined if you try to delete[] something that isn’t an array or try to delete an array, or free something that was new’ed, etc.

6 Initialization vs Assignment
#define MAXSIZE 3 class IntArrayList { public: IntArrayList() : array_(new int[MAXSIZE]), len_(0), maxsize_(MAXSIZE) { } IntArrayList(const int *const arr, size_t len) : len_(len), maxsize_(len_*2) { array_ = new int[maxsize_]; memcpy(array_, arr, len * sizeof(int)); } IntArrayList(const IntArrayList &rhs) { len_ = rhs.len_; maxsize_ = rhs.maxsize_; memcpy(array_, rhs.array_, maxsize_ * sizeof(int)); ... private: int *array_; size_t len_; size_t maxsize_; }; (5m) Let’s take a look at this class, you’ll notice a couple of things in the different constructors: Initializer lists, basically tells the compiler what to initialize fields with If you instead write assignment statements the field will be initialized and then assigned to the given value This class has its own dynamically allocated data. (TODO: show the destructor?)

7 Memory diagram See: wrapmain.cc && IntArrayList.h
What does memory look like when you call the default constructor? How about the copy constructor? Alright so let’s draw the memory diagram for an example program that uses this class. (Go back), how does this look like in memory when the default constructor is called? Gdb tricks i lo – info locals Display lo – display locals at each step (One )

8 Memory diagram int main() { Wrap a; Wrap b(new IntArrayList); struct List c { }; struct List d {*b.p()}; a = b; c = d; Wrap *e; e = &a; Wrap *f = new Wrap(&d.v); struct List *g = new struct List; g->v = *(new IntArrayList); delete f; delete g; return 0; } class Wrap { public: Wrap() : p_(nullptr) { } Wrap(IntArrayList *p) : p_(p) { *p_ = *p; } IntArrayList *p() const {return p_;} private: IntArrayList *p_; } struct List { IntArrayList v; (Draw the diagram (10m), then step through it using gdb (5m)) TODO: On c = d; was the destructor called? Ie. Did we just leak memory or was the destructor called? (Guess: Destructor was called since d.v is a stack allocated IntArrayList object, but I may be wrong) Also inspect behavior after setting Wrap *f (One of the malloc errors is from valgrind itself) …. Well, that was overwhelming, and my guess is that you guys won’t have a problem as difficult as this one. This took me a while as I was preparing for sections. Let’s run this through gdb to make sure everything we said is correct.

9 Operator Overloading A form of polymorphism.
Give special meanings to operators in user-defined classes Special member functions in classes with a particular naming convention For E.g., for overloading the ‘+’ operator, define a member function named operator+ (3 m)

10 Common operators The most commonly overloaded operators are
= (assignment operator) + - * (binary arithmetic operators) += -= *= (compound assignment operators) == != (comparison operators) (2m) If you talk about templates, Templates: there can be dangerous type inference -> namely letting it infer when you pass in a char * even though you think it’d infer it is a string TODO: Create exercises Test their memory diagram skills with something like the example (1/2 as long) you wrote (7m + 3m to go over the solution) Test their STL skills

11 Exercise 1 A) Create a Memory Diagram for the following code:
int main() { IntArrayList a; IntArrayList *b = new IntArrayList(); struct List l { a }; struct List m { *b }; Wrap w(b); delete b; } B) Identify any potential leaks (if any) Partner up with someone who has a laptop and come up with the memory diagram for this piece of code

12 Exercise 2 Modify wrapmain.cc and IntArrayList.h such that there are no memory leaks in wrapmain.cc


Download ppt "CSE 333 – Section 5 C++ (1m)."

Similar presentations


Ads by Google