CSE333 SECTION 7. Midterm Debrief Hex View 1. Find a hex editor. 2. Learn ‘goto offset’ command. 3. See HW3 pictures. The header: Magic word Checksum.

Slides:



Advertisements
Similar presentations
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
CSE333 SECTION 7. Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Learners Support Publications Classes and Objects.
Pointers OVERVIEW.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 12/6/2006 Lecture 24 – Profilers.
C++ Memory Overview 4 major memory segments Key differences from Java
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Overview of C++ Polymorphism
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 2006 Pearson Education, Inc. All rights reserved Templates.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Learning Objectives Pointers as dada members
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
C++ Memory Management Idioms
Making Dynamic Memory Allocation Safer
HW3 Hex View Inheritance Constructors/Destructors
Inheritance, Polymorphism and the Object Memory Model
Finally! Discussing a language!
CSE 333 – Section 7 HW3 Hex View Inheritance Constructors/Destructors
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CSE 333 – Section 7 HW3 Hex View Inheritance Constructors/Destructors
Concurrency Wrap-Up & Final Review
Classes and Objects.
CISC/CMPE320 - Prof. McLeod
CSE 333 – Section 10 Final Review.
Queues Jyh-Shing Roger Jang (張智星)
Objects Managing a Resource
Program Execution in Linux
Dynamic Memory Allocation
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Automating Memory Management
Presentation transcript:

CSE333 SECTION 7

Midterm Debrief

Hex View 1. Find a hex editor. 2. Learn ‘goto offset’ command. 3. See HW3 pictures. The header: Magic word Checksum Doctable size Index size

Hex View The doctable

Hex View The index

Hex View The docID table

Templates class IntPair { public: IntPair(const int first, const int second) : first_(first), second_(second) { } int first() const { return first_; } int second() const { return second_; } private: int first_; int second_; }; class DoublePair { public: DoublePair(const double first, const double second) : first_(first), second_(second) { } double first() const { return first_; } double second() const { return second_; } private: double first_; double second_; };

Templates class FooPair { public: FooPair(const Foo& first, const Foo& second) : first_(first), second_(second) { } Foo first() const { return first_; } Foo second() const { return second_; } private: Foo first_; Foo second_; }; This is really repetitive!

Templates template class Pair { public: Pair(const T& first, const T& second) : first_(first), second_(second) { } T first() const { return first_; } T second() const { return second_; } private: T first_; T second_; };

Templates Functions can be templated too Each “type” of template class/function generates distinct code Pair and Pair are two distinct classes with code located in two distinct regions of the binary Templates are generated at compile time Compiler needs to know how each template will be used Full definitions of templated code must be included in translation unit

Standard Template Library C++ comes with a rich set of templated collections cplusplus.com cppreference.com All collections pass by value (copy), not by reference Automatic resizing of a collection can trigger multiple copy operations One way to make this more efficient: move semantics Outside the scope of this class, but ask Sunjay about it any time Another way to avoid this: pass in pointers to data Memory management gets messy Use smart pointers!

Smart Pointers Encapsulate memory management through ctors/dtors Wraps a “normal” pointer Automatically calls delete when lifetime is over Three types: unique_ptr ensures only one pointer to underlying data Does this by disallowing copy construction/assignment You can still use it in STL containers though (move semantics!) shared_ptr keeps a reference count Only deletes wrapped pointer when reference count hits zero weak_ptr does not contribute to the reference count Think circular linked lists, you’d want a weak_ptr at the end of the list to ensure the reference count to the front can go down to 0. Very rarely used otherwise

Smart Pointer Examples unique_ptr.cc shared_ptr_leaky.cc shared_ptr_good.cc

Inheritance Examples Example: destructex.cc This code compiles with no warnings so it must be right?

Vtables Dynamic dispatch All virtual functions are stored in a “virtual function table” Each class has its own vtable Each instance contains an extra “field” Pointer to class vtable Only exists if class has virtual methods Derived classes have functions in same order as base class Overriding functions replace base functions at same indices

Vtable Example class Base { virtual void other_fn(); virtual void overridden(); }; class Derived { void overridden() override; }; class Base vtable Base::other_fn() Base::overridden() class Derived vtable Base::other_fn() Derived::overridden()

Vtable Example Example: vtable.cc Poke around this code with objdump or gdb!