35
05 | Pointers and RAII Kate Gregory | Gregory Consulting James McNellis | Senior Engineer, Visual C++

5 raii

Embed Size (px)

Citation preview

05 | Pointers and RAII

Kate Gregory | Gregory Consulting

James McNellis | Senior Engineer, Visual C++

1. Pointers

2. Dynamic Allocation

3. Exceptions

4. Copying, Assignment, and Destruction

5. Resource Acquisition is Initialization (RAII)

6. Smart Pointers

Module Overview

1. Pointers

2. Dynamic Allocation

3. Exceptions

4. Copying, Assignment, and Destruction

5. Resource Acquisition is Initialization (RAII)

6. Smart Pointers

Module Overview

……

Lesson 1: Pointers

void f()

{

int x{1};

int y{2};

}

x y

……

Lesson 1: Pointers

void f()

{

int x{1};

int y{2};

}

x y

&y&x

Lesson 1: Pointers

void f()

{

int x{1};

int y{2};

int* pointer_to_x{&x};

int* pointer_to_y{&y};

}

Lesson 1: Pointers

void f()

{

int x{1};

int* pointer_to_x{&x};

int y{*pointer_to_x};

}

Lesson 1: Pointers

void f()

{

int x{1};

int* pointer_to_x{&x};

*pointer_to_x = 2;

}

Lesson 1: Pointers

void f()

{

int x{1};

int y{2};

int* p{&x};

p = &y;

}

Lesson 1: Pointers

void f()

{

int p{nullptr};

}

DEMOPointers

1. Pointers

2. Dynamic Allocation

3. Exceptions

4. Copying, Assignment, and Destruction

5. Resource Acquisition is Initialization (RAII)

6. Smart Pointers

Module Overview

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?

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

The Free Store

void f()

{

int* p{new int{1}};

*p = 2;

delete p;

}

DEMOPointers

1. Pointers

2. Dynamic Allocation

3. Exceptions

4. Copying, Assignment, and Destruction

5. Resource Acquisition is Initialization (RAII)

6. Smart Pointers

Module Overview

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

Exception syntax

try

{

// anything, including function calls

}

catch (const std::exception& e)

{

std::cout << "Exception caught: " << e.what();

}

DEMOExceptions

1. Pointers

2. Dynamic Allocation

3. Exceptions

4. Copying, Assignment, and Destruction

5. Resource Acquisition is Initialization (RAII)

6. Smart Pointers

Module Overview

void f()

{

int x{1};

int y{x}; // Copying

y = x; // Assignment

}

Copying, Assignment, and Destruction

void f()

{

Rectangle x{3, 4};

Rectangle y{x}; // Copying

y = x; // Assignment

}

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

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}

{}

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

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;

}

Copying, Assignment, and Destruction

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

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

Memory released

DEMORAII

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

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

1. Pointers

2. Dynamic Allocation

3. Exceptions

4. Copying, Assignment, and Destruction

5. Resource Acquisition is Initialization (RAII)

6. Smart Pointers

Module Overview

DEMOSmart Pointers

©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.