Skip to content

Commit 3087836

Browse files
committed
Update Chapter 5
1 parent 401f1fd commit 3087836

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

week05/Lecture05.pptx

16.4 KB
Binary file not shown.

week05/examples/memoryleak.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void foo()
5+
{
6+
int* p = (int *) malloc( sizeof(int));
7+
return;
8+
} //memory leak
9+
10+
int main()
11+
{
12+
int * p = NULL;
13+
14+
p = (int *) malloc(4 * sizeof(int));
15+
// some statements
16+
p = (int *) malloc(8 * sizeof(int));
17+
// some statements
18+
free (p);
19+
// the first memory will not be freed
20+
21+
for(int i = 0; i < 1024; i++)
22+
{
23+
p = (int *) malloc(1024 * 1024 * 1024);
24+
}
25+
printf("End\n");
26+
27+
return 0;
28+
}

week05/examples/newdelete.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct Student{
5+
char name[4];
6+
int born;
7+
bool male;
8+
};
9+
10+
int main()
11+
{
12+
//allocate an int, default initializer (do nothing)
13+
int * p1 = new int;
14+
//allocate an int, initialized to 0
15+
int * p2 = new int();
16+
//allocate an int, initialized to 5
17+
int * p3 = new int(5);
18+
//allocate an int, initialized to 0
19+
int * p4 = new int{};//C++11
20+
//allocate an int, initialized to 5
21+
int * p5 = new int {5};//C++11
22+
23+
//allocate a Student object, default initializer
24+
Student * ps1 = new Student;
25+
//allocate a Student object, initialize the members
26+
Student * ps2 = new Student {"Yu", 2020, 1}; //C++11
27+
28+
//allocate 16 int, default initializer (do nothing)
29+
int * pa1 = new int[16];
30+
//allocate 16 int, zero initialized
31+
int * pa2 = new int[16]();
32+
//allocate 16 int, zero initialized
33+
int * pa3 = new int[16]{}; //C++11
34+
//allocate 16 int, the first 3 element are initialized to 1,2,3, the rest 0
35+
int * pa4 = new int[16]{1,2,3}; //C++11
36+
37+
//allocate memory for 16 Student objects, default initializer
38+
Student * psa1 = new Student[16];
39+
//allocate memory for 16 Student objects, the first two are explicitly initialized
40+
Student * psa2 = new Student[16]{{"Li", 2000,1}, {"Yu", 2001,1}}; //C++11
41+
cout << psa2[1].name << endl;
42+
cout << psa2[1].born << endl;
43+
44+
//deallocate memory
45+
delete p1;
46+
//deallocate memory
47+
delete ps1;
48+
49+
//deallocate the memory of the array
50+
delete pa1;
51+
//deallocate the memory of the array
52+
delete []pa2;
53+
54+
//deallocate the memory of the array, and call the destructor of the first element
55+
delete psa1;
56+
//deallocate the memory of the array, and call the destructors of all the elements
57+
delete []psa2;
58+
59+
return 0;
60+
}

week05/examples/stack-heap.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int a = 0;
7+
int b = 0;
8+
int c = 0;
9+
10+
cout << &a << endl;
11+
cout << &b << endl;
12+
cout << &c << endl;
13+
14+
int * p1 = (int*) malloc (4);
15+
int * p2 = (int*) malloc (4);
16+
int * p3 = (int*) malloc (4);
17+
18+
cout << p1 << endl;
19+
cout << p2 << endl;
20+
cout << p3 << endl;
21+
22+
free(p1);
23+
free(p2);
24+
free(p3);
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)