% lec 14: smart pointers # today's plan STL (cont.) smart pointers & RAII C/C++ wrapup # self-exercise 1 push 1 to 10 to `std::vector` compute the sum . . . [gdb, performance, size/reserve, accumulate, list] . . . product of from 1 to 10? # accumulate ```c++ #include #include #include #include int main() { std::vector vec; for (int i = 1; i <= 10; ++i) vec.push_back(i); int prod = std::accumulate(vec.begin(), vec.end(), 1, std::multiplies()); std::cout << prod << std::endl; } ``` [function object](http://en.cppreference.com/w/cpp/utility/functional) [lambda function](http://en.cppreference.com/w/cpp/language/lambda): `[](int x, int y) { return x * y; }` generic lambda (C++ 14, gcc 4.9+): use `auto` # self-exercise 2 read in names & phone numbers query by name ```c Alice 206.555.1234 Bob 206.555.7890 ``` [phonebook.cc](l14/phonebook.cc) # smart pointers C++ classes that mimic pointers C++ 98: `auto_ptr` - deprecated C++ 11: `unique_ptr`, `shared_ptr`, `weak_ptr` others: [boost](http://www.boost.org/doc/libs/release/libs/smart_ptr/smart_ptr.htm), [Chrome](http://www.chromium.org/developers/smart-pointer-guidelines), [WebKit](https://www.webkit.org/coding/RefPtr.html) # problem: resource management how to free resources? example 1: HW1 example 2: [kexec](https://github.com/torvalds/linux/blob/master/kernel/kexec.c#L562) . . . easy to miss; complicated control flow; repeated free/delete # unique_ptr take ownership of a pointer can be moved; cannot be copied unique_ptr's destructor invokes delete on the owned pointer ```c++ #include #include int main() { std::unique_ptr x(new int(5)); // C++ 14: auto x = std::make_unique(5); (*x)++; std::cout << *x << std::endl; // delete when the lifetime of x ends } ``` # RAII Resource Acquisition Is Initialization not limited to new/delete ```c++ template< class T, class Deleter = std::default_delete > class unique_ptr; ``` ```c++ #include #include int main() { std::unique_ptr fp(fopen("foo.txt", "rb"), fclose); ... } ``` `decltype`: `typeof` # type casting - C explicit casts: `(type)expr` - [C++ explicit casts](http://en.cppreference.com/w/cpp/language/explicit_cast) - `const_cast(expr)` - `static_cast(expr)` - `reinterpret_cast(expr)` - `dynamic_cast(expr)` `dynamic_cast`: java-like run-time check won't work if [run-time type information (RTTI) disabled](https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Run-Time_Type_Information__RTTI_) # C/C++ projects coding style: [Google](https://google-styleguide.googlecode.com/svn/trunk/cppguide.html), [Linux kernel](https://github.com/torvalds/linux/blob/master/Documentation/CodingStyle), [LLVM](http://llvm.org/docs/CodingStandards.html), [Mozilla](https://developer.mozilla.org/en-US/docs/Mozilla/C++_Portability_Guide), [clang-format](http://clang.llvm.org/docs/ClangFormat.html) version control: [git](http://git-scm.com/) ([CSE GitLab](https://gitlab.cs.washington.edu/users/sign_in)), [mercurial](http://mercurial.selenic.com/), [subversion](https://subversion.apache.org/) build system: [GNU make](http://www.gnu.org/software/make/), [CMake](http://www.cmake.org/), [Ninja](http://martine.github.io/ninja/) analysis: [cpplint](http://google-styleguide.googlecode.com/svn/trunk/cpplint/cpplint.py), [clang static analyzer](http://clang-analyzer.llvm.org/), [sparse](https://sparse.wiki.kernel.org/) debugging: [valgrind](http://valgrind.org/), [AddressSanitizer](https://code.google.com/p/address-sanitizer/) unit-testing: [gtest](https://code.google.com/p/googletest/) # see you on Friday! guest lecture on [Rust](http://www.rust-lang.org/)