Top Banner
CS32 Discussion Week 2 Muhao Chen [email protected] http://yellowstone.cs.ucla.edu/~muhao / 1
48

CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Jul 16, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

CS32 DiscussionWeek 2

Muhao Chen

[email protected]

http://yellowstone.cs.ucla.edu/~muhao/

1

Page 2: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

About me and CS32

Muhao Chen

• Ph.D. Candidate, working with Prof. Carlo Zaniolo and Prof. Kai-Wei Chang

• Research interest: natural language processing, machine learning, and spatiotemporal reasoning

• Teaching Fellow

• Homepage: http://yellowstone.cs.ucla.edu/~muhao/ (slides will be posted here)

2

CS32

• Mostly about data structures, and some object-oriented design.

• Basic knowledge of computational complexity analysis.

Page 3: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

My Recent Research

• Representation learning of knowledge graphs• How do we obtain quantified representations for commonsense

knowledge and incorporate them into machine learning?

• Semi-supervised learning• Matching concepts across multiple human languages with limitedly

known cross-lingual alignment

• Machine reading comprehension• Predict what happens next in short stories

3

• I am working with some undergraduate researchers• Pei Zhou (15 Math Comp.): leading author of an ICWSM’18 paper on

computational modeling of linguistic variation

• Haiying Huang (16EE), Ryan Liu (16 Math Comp.), Xian Kai Ng (16CS): Machine reading comprehension

Page 4: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Outline

•Dynamic memory allocation revisit

•Copy Constructor

•Assignment Overloading

•Linked Lists

4

Page 5: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Dynamic Memory Allocation

5

Page 6: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Static memory allocation

• If we want to type in a paragraph and save it into a C-string.

• #define MAXLENGTH 10000• char s[MAXLENGTH+1]; cin.getline(s);

• What if the paragraph is extremely long?• out-of-bound

• What if the paragraph has only five words?• Over-allocated memory

6

Page 7: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Dynamic allocation

• What if we want to fit the paragraph into a C-string with right the sufficient size?

• Dynamic allocation of an array• <type> *<name> = new <type>[<#elements>];• char *article = new char[length];

7

int length;

cout << how many characters are at most in your article? << endl;

cin >> length;

char *article;

if (length >0)

article = new char[length + 1];

Unsigned int variable

Page 8: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

new

• new will dynamically allocate the sequential memory space for the requested data type and size.

• new will always return the starting address of the allocated memory space.

• int array=new int[size]; X

• int *array = new int[size]; ✓

• Anything allocated with new will remain in the memory unless we manually delete it.

8

Page 9: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

What if we want to dynamically allocate a 2-D array

9

int rows = 5; int cols = 20;

int **array = new int*[rows];

for (int i=0; i<rows; ++i)

array[i] = new int[cols];

//array is now array[5][20]

Page 10: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Memory Leak

10

int *p;

p = new int[2000000];

p = new int[1000000];

• We allocate 2M blocks of int and point p to it.

• Then we allocate another 1M and point p to it. p no

longer points to the first 1M blocks.

• The first 2M blocks of int become the “memory ghost”.

We no longer can access and release it.

• This situation is called Memory Leak.

2M of integers remain in the memory because they are not manually deleted.

Page 11: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Delete

Dynamic allocation has to be deleted once we no longer use it.

delete[] p;

• Delete the dynamic array pointed by p.

11

Page 12: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Copy Constructor

12

Page 13: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

13

Page 14: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

14

Page 15: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

15

Page 16: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

16

Page 17: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

The Problem Here: Shallow Copy

• When we copy C-strings:

2006 Corvette Stingray

• Deep copy: grab every character from a to b:

17

char a[100] = “2016 Corvette Stingray”;char b[100];b = a;a[2] = ‘0’;cout << b;

for (int i = 0; i <= strlen(a); ++i)b[i] = a[i];

Page 18: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

18

Page 19: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

19

Page 20: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Copy Constructors

• A constructor to define the behavior of copying from one instance to another.

20

Page 21: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

School::School(const School &aSchool) {m_name = aSchool.m_name;m_numStudents = aSchool.m_numStudents;m_students = new Students[m_numStudents];for (int i = 0; i < m_numStudents; ++i)

m_students[i] = aSchool.m_students[i];}

21

Immutable. We don’t change the object we’re copying from.

Page 22: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

22

Page 23: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

23

Page 24: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

24

Page 25: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

25

Othewise think ofA=B=C; //A?

Page 26: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

26

Page 27: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

27

Page 28: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

28

Page 29: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

29

Page 30: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

30

Page 31: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

31

Page 32: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

32

Page 33: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

33

Page 34: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

34

Page 35: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

35

Page 36: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

36

Page 37: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

• What about insertion in the middle of the list?

• At the end of the list?

37

End (q): q->next = p; p -> next = NULL;Middle (after q): p -> next = q -> next; q -> next = p;

Page 38: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

38

Node* Search(int key, Node* head){Node *q = head;while(q != NULL) {

if(q -> value != key) q = q -> next; //move to nextelse return q;

}return NULL;

}

Page 39: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

39

Page 40: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

40

Page 41: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

41

Page 42: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

42

Page 43: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

43

void remove(int valToRemove, Node* head) {Node *p = head, *q = NULL;

while (p != NULL) {if (p->value == valToRemove)

break;q = p; //q keeps track the previous of pp = p->next;}

if (p == NULL)return;

if (p == head) //node to remove is headhead = p->next;

elseq->next = p->next;

delete p;}

Page 44: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

44

Page 45: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

45

Page 46: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

46

Page 47: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

A Classic Problem about Circularly Linked List

• Given a head node of a linked list, how to verify whether there is a loop in the linked list or not?

47

Page 48: CS32 Discussion Week 2yellowstone.cs.ucla.edu/~muhao/cs32s18/week2.pdf · 2018-01-19 · Memory Leak 10 int *p; p = new int[2000000]; p = new int[1000000]; • We allocate 2M blocks

Bugs in your software are actually special features :)

48