-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_Cpp_LearningNotes
147 lines (109 loc) · 2.67 KB
/
C_Cpp_LearningNotes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
answer.txt
What’s 7 times 6?
#ifdef REVEAL
42
#endif
cpp -P answer.txt
output:
What’s 7 times 6?
cpp -P -D REVEAL answer.txt
output:
What’s 7 times 6?
42
gcc -E hello. ==> preprocessing
gcc -c main.c answer.c ==> Complie ==> main.o answer.o
gcc -o prog main.o answer.o ==> Link ==> get a program ./prog
---debug.c
#include <stdio.h>
int main() {
#ifdef DEBUG
printf("Hello, World!\n");
#endif
return 0; }
gcc -DDEBUG debug.c -o debug
Lecture 2
Anonymous Blocks:
Anonymous blocks demonstrate the concept of block scope.
void foo(){
{ int a = 0; }
{
double a = 3.14; // no problem!
{
char * a = "3.14"; // no problem!
}
}
// no 'a' defined in this scope
}
Lecture 3
- Memory
- Heap is a chunk of memory that users can use to dynamically allocated memory. Lasts until freed, or program exits.
- Stack contains local variables from functions and related book-keeping data. LIFO structure.
- Function variables are pushed onto stack when called.
- Functions variables are popped off stack when return.
- Pointer:Pointers are integer variables themselves, so can have pointer to pointers: char **ptr;
Lecture 4
推荐使用Valgrind
Struct Memory
- Struct size != sum of member sizes
- All members must “align” with largest member size
- Each member has own alignment requirements
Ex: char = 1-byte aligned.
short = 2-byte aligned.
int = 4-byte aligned. ←Refer to documentation
Blackboard Example:
struct x{
char a; //1-byte, must be 1-byte aligned
short b; //2-bytes, must be 2-byte aligned
int c; // Biggest member (4-bytes). X must be 4-byte // aligned
char d;
}
Unions: Can only access one member at a time. Union stores all data in same chunk of memory.
Lecture 5: C++
Complier: g++ > similar to gcc
g++ -o test test.cpp
new memory management :
int * numArray = new int[100]; (取代了malloc)
delete numArray; (取代了free)
struct foo * bar = new struct foo;
Constructor v.s. Destructor
- Since we explicitly allocated something with new, we must also explicitly de-allocate it.
Template
Subclass: inherit fields from the parent
Inheritence
class Shape {
public:
void draw();
};
class Circle : public Shape {
public:
int getRadius();
};
int main() {
Circle circle;
circle.draw();
}
Is-a
class Circle
: public Shape {
public:
int radius;
};
==>circle.x
Has-a
class Circle {
public:
Shape shape;
int radius;
};
==?circle.shape.x
private == only that class
protected == only that class and subclass
-->protected v.s. private
virtual: virtual function is determined at run-time;
non-virtual: function is determined at compile-time;
destructor:
struct Buffer {
Buffer(int s) { buf = new char[s]; }
~Buffer() { delete [] buf; }
char *buf;
};