0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# 5 raii

64

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
64
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. 05 | Pointers and RAII Kate Gregory | Gregory Consulting James McNellis | Senior Engineer, Visual C++
• 2. Module Overview 1. Pointers 2. Dynamic Allocation 3. Exceptions 4. Copying, Assignment, and Destruction 5. Resource Acquisition is Initialization (RAII) 6. Smart Pointers
• 3. Module Overview 1. Pointers 2. Dynamic Allocation 3. Exceptions 4. Copying, Assignment, and Destruction 5. Resource Acquisition is Initialization (RAII) 6. Smart Pointers
• 4. Lesson 1: Pointers void f() { int x{1}; int y{2}; } … x y …
• 5. Lesson 1: Pointers void f() { int x{1}; int y{2}; } &x … &y x y …
• 6. Lesson 1: Pointers void f() { int x{1}; int y{2}; int* pointer_to_x{&x}; int* pointer_to_y{&y}; }
• 7. Lesson 1: Pointers void f() { int x{1}; int* pointer_to_x{&x}; int y{*pointer_to_x}; }
• 8. Lesson 1: Pointers void f() { int x{1}; int* pointer_to_x{&x}; *pointer_to_x = 2; }
• 9. Lesson 1: Pointers void f() { int x{1}; int y{2}; int* p{&x}; p = &y; }
• 10. Lesson 1: Pointers void f() { int p{nullptr}; }
• 11. DEMO Pointers
• 12. Module Overview 1. Pointers 2. Dynamic Allocation 3. Exceptions 4. Copying, Assignment, and Destruction 5. Resource Acquisition is Initialization (RAII) 6. Smart Pointers
• 13. Lesson 2: Dynamic Allocation • Objects have scope, and go away when execution reaches the end of that scope void f() { NamedRectangle fred_rectangle{ "Fred", 3, 4 }; // . . . } • What if other code needed that object still?
• 14. The Free Store • Separate area of memory • Objects created there stick around until deliberately cleaned up • To create one, use new – returns a pointer • To clean up, use delete
• 15. The Free Store void f() { int* p{new int{1}}; *p = 2; delete p; }
• 16. DEMO Pointers
• 17. Module Overview 1. Pointers 2. Dynamic Allocation 3. Exceptions 4. Copying, Assignment, and Destruction 5. Resource Acquisition is Initialization (RAII) 6. Smart Pointers
• 18. Exceptions • Sometimes what you want can’t be done – That file doesn’t exist any more – You don’t have permission to do that to the system • Checking in advance is best, but not always possible • Code that calls a function doesn’t need to check return value • C++ provides a non ignorable signal that something awful has happened • Will return from any nested level of function calls to a place that can handle the error
• 19. Exception syntax try { // anything, including function calls } catch (const std::exception& e) { std::cout << "Exception caught: " << e.what(); }
• 20. DEMO Exceptions
• 21. Module Overview 1. Pointers 2. Dynamic Allocation 3. Exceptions 4. Copying, Assignment, and Destruction 5. Resource Acquisition is Initialization (RAII) 6. Smart Pointers
• 22. Copying, Assignment, and Destruction void f() { int x{1}; int y{x}; // Copying y = x; } // Assignment
• 23. Copying, Assignment, and Destruction void f() { Rectangle x{3, 4}; Rectangle y{x}; // Copying y = x; } // Assignment
• 24. Copying, Assignment, and Destruction • Copying a class-type object invokes a special constructor – Called the copy constructor – Rectangle(Rectangle const&); – If you don’t define one, the compiler provides one by default • The default behaviour is to copy each base and data member
• 25. Copying, Assignment, and Destruction • Copying a class-type object invokes a special constructor – Called the copy constructor – Rectangle(Rectangle const&); – If you don’t define one, the compiler provides one by default • The default behaviour is to copy each base and data member Rectangle(Rectangle const& other) : _width{other._width}, _height{other._height}
• 26. Copying, Assignment, and Destruction • Assigning to a class-type object invokes a special operator – Called the assignment operator – Rectangle& operator=(Rectangle const&); – If you don’t define one, the compiler provides one by default • The default behaviour is to assign each base and data member
• 27. Copying, Assignment, and Destruction • Assigning to a class-type object invokes a special operator – Called the assignment operator – Rectangle& operator=(Rectangle const&); – If you don’t define one, the compiler provides one by default • The default behaviour is to assign each base and data member Rectangle& operator=(Rectangle const& other) { _width = other._width; _height = other._height; return *this; }
• 28. Copying, Assignment, and Destruction void f() { int x{1}; // x comes into existence int y{2}; // y comes into existence } // y goes out of existence // x goes out of existence
• 29. Destructor • Name is always ~ plus the name of the class – E.g., ~Rectangle() • Returns nothing, takes no parameters • Place to cleanup resources used by an object – Close a file – Release a lock – Etc • When exceptions transfer execution out of a scope, everything declared in that scope is cleaned up – Destructor runs
• 30. DEMO RAII
• 31. Smart pointers • unique_ptr if only one object needs access to the underlying pointer • shared_ptr if several want to use the same underlying pointer – Cleaned up when the last copy goes out of scope • In <memory> header file • If you’re using new or delete, you’re doing it wrong
• 32. RAII in General • Constructor acquires resource – Eg opens file • All other member functions know resource is acquired – Do not need to test and make sure • Destructor releases resource – Works even in the presence of exceptions
• 33. Module Overview 1. Pointers 2. Dynamic Allocation 3. Exceptions 4. Copying, Assignment, and Destruction 5. Resource Acquisition is Initialization (RAII) 6. Smart Pointers
• 34. DEMO Smart Pointers
• 35. ©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.