CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy...
Transcript of CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy...
![Page 1: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/1.jpg)
CSE 333 Section AB
C++ classes & dynamic memory! (w/ Yifan & Travis)
1
![Page 2: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/2.jpg)
Logistics
Due TONIGHT:Homework 2 @ 9 pm
Due Monday:Exercise 12 @ 11 am
Due Wednesday 10/24:Exercise 12a @ 11 am
2
!!!! Midterm next weekFriday November 1st!!!!
![Page 3: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/3.jpg)
C++ continued
C++ ClassesMemory Dynamism
3
![Page 4: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/4.jpg)
Questions and review● What do the following modifiers mean?
− public: − protected:− private:− friend:
● What is a struct under this new context?
Member is accessible by anyoneMember is accessible by this class and any derived classes
Member is only accessible by this classAllows access of private/protected members to other functions and/or classes
− A struct can be thought of as a class where all members are default public instead of default private. In C++, it is also possible to give member functions (such as a constructor) to structs
4
![Page 5: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/5.jpg)
When we assign a struct variable to another, what happens when the structure contains an array?
struct vector{
double coords[3];
int id;
}
qtcoords= [3, 1, 4]id = 1
− Compiler automatically performs Deep Copy for array members
− Same behaviour for arrays in classes
Origincoords= [0.0, 0.0, 0.0]id = 2
Origin = qt
Origincoords= [3, 1, 4]id = 1
qtcoords= [3, 1, 4]id = 1
5
![Page 6: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/6.jpg)
Constructors Revisited
● Constructor (ctor): Can define any number as long as they have different parameters. Constructs a new instance of the class.
class Int { public: Int() { ival = 17; cout << "default(" << ival << ")" << endl; } Int(int n) { ival = n; cout << "ctor(" << ival << ")" << endl; } Int(const Int &n) {
ival = n.ival; cout << "cctor(" << ival << ")" << endl;
} ~Int() { cout << "dtor(" << ival << ")" << endl; } …};
Constructor (ctor) Constructor (ctor)
● Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!). Invoked when passing/returning a non-reference object to/from a function.
Copy Constructor (cctor)
● Destructor (dtor): Cleans up the class instance. Deletes dynamically allocated memory (if any).
Destructor (dtor)
6
![Page 7: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/7.jpg)
Constructors Revisited
● Constructor (ctor): Can define any number as long as they have different parameters. Constructs a new instance of the class.
class Int { public: Int() { ival = 17; cout << "default(" << ival << ")" << endl; } Int(int n) { ival = n; cout << "ctor(" << ival << ")" << endl; } Int(const Int &n) {
ival = n.ival; cout << "cctor(" << ival << ")" << endl;
} ~Int() { cout << "dtor(" << ival << ")" << endl; } …};
Constructor (ctor) Constructor (ctor)
● Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!). Invoked when passing/returning a non-reference object to/from a function.
Copy Constructor (cctor)
● Destructor (dtor): Cleans up the class instance. Deletes dynamically allocated memory (if any).
Destructor (dtor)
7
![Page 8: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/8.jpg)
8
class Int { public: Int() { ival_ = 17; cout << "default(" << ival_ << ")" << endl; } Int(int n) { ival_ = n; cout << "ctor(" << ival_ << ")" << endl; } Int(const Int &n) { ival_ = n.ival_; cout << "cctor(" << ival_ << ")" << endl; } ~Int() { cout << "dtor(" << ival_ << ")" << endl; } int get() const { cout << "get(" << ival_ << ")" << endl; return ival_; } void set(int n) { ival_ = n; cout << "set(" << ival_ << ")" << endl; } private: int ival_;};
int main(int argc, char **argv) { Int p; Int q(p); Int r(5); q.set(p.get()+1); return EXIT_SUCCESS;}
![Page 9: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/9.jpg)
9
int main(int argc, char **argv) { Int p; Int q(p); Int r(5); q.set(p.get()+1); return EXIT_SUCCESS;}class Int { public: Int() { ival_ = 17; cout << "default(" << ival_ << ")" << endl; } Int(int n) { ival_ = n; cout << "ctor(" << ival_ << ")" << endl; } Int(const Int &n) { ival_ = n.ival_; cout << "cctor(" << ival_ << ")" << endl; } ~Int() { cout << "dtor(" << ival_ << ")" << endl; } int get() const { cout << "get(" << ival_ << ")" << endl; return ival_; } void set(int n) { ival_ = n; cout << "set(" << ival_ << ")" << endl; } private: int ival_;};
UwU Looks like we got all the function calls!
![Page 10: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/10.jpg)
Questions and review
● What is the destruction order?
int main(int argc, char **argv) { Int p; Int q(p); Int r(5); q.set(p.get()+1); return EXIT_SUCCESS;}
10
Destruction order is the reverse of construction
order.
![Page 11: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/11.jpg)
11
![Page 12: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/12.jpg)
Questions and review● What happens if you don’t define a copy constructor? Or an assignment operator? Or
a destructor? Why might this be bad?
● (Hint: What if a member of a class is a pointer to heap-allocated struct?)
● How can you disable the copy constructor/assignment operator/destructor?12
In C++, if you don’t define any of these, a default one will be synthesized
for you.
The default copy constructor does a shallow copy of all fields.
The default assignment operator does a shallow copy of all fields.
The default destructor calls the default destructors of any fields that have
them.
Set their prototypes equal to the keyword “delete”: ~SomeClass() = delete;
![Page 13: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/13.jpg)
Call Stack:
baz(1, 2, 3)
13
![Page 14: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/14.jpg)
Call Stack:
baz(1, 2, 3)
foo(2, 3)
h14
![Page 15: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/15.jpg)
Call Stack:
baz(1, 2, 3)
bar(1)
h a
foo(1)
15
![Page 16: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/16.jpg)
Call Stack:
baz(1, 2, 3)
bar(1)
h a
foo()
p16
![Page 17: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/17.jpg)
Call Stack:
baz(1, 2, 3)
bar(1)
h a p p17
![Page 18: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/18.jpg)
Call Stack:
baz(1, 2, 3)
h a p p i18
![Page 19: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/19.jpg)
Call Stack:
~baz()
h a p p i n19
![Page 20: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/20.jpg)
Call Stack:
~bar()
h a p p i n e20
![Page 21: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/21.jpg)
Call Stack:
~foo()
h a p p i n e s21
![Page 22: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/22.jpg)
Call Stack:
~foo()
h a p p i n e s s22
![Page 23: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/23.jpg)
Call Stack:
~foo()
h a p p i n e s s s23
![Page 24: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/24.jpg)
24
How many bytes of memory are leaked by this program?
![Page 25: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/25.jpg)
25
How many bytes of memory are leaked by this program?
12 bytes
![Page 26: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/26.jpg)
26
How can we fix these memory leaks?
![Page 27: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/27.jpg)
27
How can we fix these memory leaks?
![Page 28: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/28.jpg)
28
![Page 29: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/29.jpg)
29
When ~BadCopy() is invoked for bc2, we will try to delete already deleted memory
![Page 30: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/30.jpg)
Question 5
30
![Page 31: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/31.jpg)
array_
Question 5
31
a array_
stack heap
int[MAXSIZE]
0x……..b array_
int[MAXSIZE]
array_vl int[MAXSIZE]
vm int[MAXSIZE]
p_ 0x……..w
![Page 32: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/32.jpg)
array_
Question 5
32
a array_
stack heap
int[MAXSIZE]
0x……..b array_
int[MAXSIZE]
array_vl int[MAXSIZE]
vm int[MAXSIZE]
p_ 0x……..w
![Page 33: CSE 333 Section AB - courses.cs.washington.edu...Constructor (ctor) Constructor (ctor) Copy Constructor (cctor): Creates a new instance based on another instance (must take a reference!).](https://reader034.fdocuments.in/reader034/viewer/2022051411/603dd95dce1c2460e1762d2e/html5/thumbnails/33.jpg)
array_
Question 5
33
a array_
stack heap
int[MAXSIZE]
0x……..b
int[MAXSIZE]
array_vl int[MAXSIZE]
vm int[MAXSIZE]
p_ 0x……..w
Still on the heap!