C++ Copy Constructors & Overloaded Assignment: Memory & Security, Summaries of Logic

The concept of copy constructors and overloaded assignment in C++, focusing on dynamic memory allocation and potential security issues. It covers when copies of objects are made, the role of copy constructors and assignment operators, and the importance of proper memory management in classes with dynamically allocated memory.

Typology: Summaries

2021/2022

Uploaded on 09/27/2022

geryle
geryle 🇺🇸

4.5

(23)

277 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 202, Version 3/02
1
Copy Constructors
and
Overloaded Assignment
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download C++ Copy Constructors & Overloaded Assignment: Memory & Security and more Summaries Logic in PDF only on Docsity!

CMSC 202, Version 3/

Copy Constructors

and

Overloaded Assignment

CMSC 202, Version 3/

When do we make copies of

an object?

1 ) When passing them to a function by value2) When returning them from a function by value3) When creating a new object that is initialized with

a copy of an existing object4) When assigning objects (x = y)

1. Items 1, 2 and 3 are handled by the copy

constructor

2. Item 4 is handled by overloading

the

assignment ( = ) operator.

CMSC 202, Version 3/

Copy constructor syntax

The function prototype for every copy

constructor is of the form:

ClassName::ClassName (const ClassName &);Why is it necessary to for this parameter to

be passed by reference? Why can’t it bepassed by value?

CMSC 202, Version 3/

Why haven’t we seen this

before?

• The compiler provides a default copy

constructor which up until now has beensufficient.

• The default copy constructor simply

copies each of the data members fromthe existing object into the new object

• This is not sufficient if one or more of

the data members points to dynamicallyallocated memory

CMSC 202, Version 3/

A potential security problem

-^

In more complex projects, you will often havepointers as private class members. It iscritical that a copy constructor allocate newmemory for each pointer in the new copy– The default copy constructor will just create a new

pointer for the copy and give it the same value asthe original pointer. So the copy and original willboth point to the same data. This is a grosssemantic error: changing the copy will change theoriginal! How can this create a serious securityproblem?

CMSC 202, Version 3/

A potential security problem

-^

Also, consider the “accessor” functions foryour class, which exist simply to report thevalue of unchangeable private data members.– Why is it a security problem if your function returns

a pointer to the data instead of a copy of the data?This can be a hard error to catch in languages likeJava, which blur the distinctions of which variablesare pointers and which are not.

-^

These are security problems, and debuggingnightmares. Be careful to avoid them.

CMSC 202, Version 3/

SmartArray

class SmartArray {

public:

SmartArray ( int size = 100 );// other members

private:

int m_size;int *m_theData;

CMSC 202, Version 3/

Using SmartArray

Some SmartArray objects:

SmartArray a1 (50);

// 50 ints

SmartArray a2 (200);

// 200 ints

SmartArray a3;

// 100 ints by default

CMSC 202, Version 3/

13

A Picture of Memory

Given the instantiation

SmartArray a1(50);

we get this picture of memory:

a

m_size m_theData

CMSC 202, Version 3/

When Does the Memory Get

Deallocated?

The intuitive answer is, “In the destructor.” The

compiler provides us with a default destructor thatdeallocates the private data members. But this isnot sufficient. If we relied on the default destructor,we’d create a memory leak

because the memory

pointed to by

m_theData

would not be freed.

SmartArray::~SmartArray (

delete [

] m_theData;

CMSC 202, Version 3/

16

Effect of Default Copy

Constructor (shallow copy)

a

-^

m_size m_theData

a

m_size m_theData

Because the default copy constructoronly copied the contents of m_theDatain^

a into m_theData in

a

, both point

to the array allocated in

a1.

(Possible

dangling pointer

or other

logic

problem!)

CMSC 202, Version 3/

17

The picture of memory we

want (deep copy)

a

-^

m_size m_theData

a

m_size m_theData

-^

CMSC 202, Version 3/

When Is the Copy Constructor

Invoked?

Silently by the compiler when we• Pass by value:void someFunction(SmartArray array);• Return by value:

SmartArray someFunction(

parameters

SmartArray temp;// code manipulating “temp”return (temp);

CMSC 202, Version 3/

When Is the Copy Constructor

Invoked? (cont’d)

  • Explicitly by us upon construction

SmartArray a1;// constructing a2 as a copy of a1SmartArray a2 = a1;

OR

SmartArray a2(a1);