-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-values-references.tex
More file actions
318 lines (288 loc) · 8.34 KB
/
02-values-references.tex
File metadata and controls
318 lines (288 loc) · 8.34 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
\input{definitions}
\begin{document}
\title{Values and References}
\subtitle{Russian Roulette with Memory}
\date{\today}
\author{Florian Warg, Max Staff}
\maketitle
\begin{frame}[fragile]
\frametitle{What are values?}
\begin{itemize}
\item values are expressions in a normal form
\item cannot be reduced or evaluated any further
\begin{lstlisting}[numbers=none]
1 + 2 // not a value (can be reduced)
3 // value (in normal form)
\end{lstlisting}
\item variables can hold values
\item value is independent from its location
\begin{lstlisting}[numbers=none]
int x = 2, y = 4, z = 4;
x + y == x + z // 2 + 4 == 2 + 4
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Lvalues}
\begin{itemize}
\item lvalues are expressions which can be used on the left side of an assignment operation (and also the right side)
\item i.e. an lvalue has a memory address
\begin{lstlisting}[numbers=none]
int x = 1; // x is evaluated to 1
x = 2; // x is lvalue
2 = x; // compiler error: 2 is not an lvalue
int y = x; // both x and y are lvalues
\end{lstlisting}
\item lvalues persist beyond a single expression
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Rvalues}
\begin{itemize}
\item rvalues are non-lvalues
\item can only be used on the right side of an assignment operation
\item e.g. temporary variables, literals, addresses are rvalues
\begin{lstlisting}[numbers=none]
/* temporaries and literals*/
"rvalue";
2;
41 + 4;
/* not rvalues */
int x = 1, y = x; // x is lvalue, not rvalue
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{References}
\begin{itemize}
\item a reference is an alias for an existing value
\item there are references for lvalues and rvalues
\item references are not necessarily objects
\item i.e. there are no pointers, arrays or references to references
\item give access to a variable without copying its value
\item allow modification of local variables in other scopes
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Lvalue references}
\begin{itemize}
\item alias which refers to an lvalue
\begin{lstlisting}
int x = 42;
int& lx = x; // create lvalue reference to x
++lx; // use alias instead of x
cout << x; // what is printed here?
\end{lstlisting}
\item functions taking lvalue references can modify local variables
\begin{lstlisting}
void add2(int& ref) { ref += 2; }
int x = 10;
add2(x); // ref is initialized with x
cout << x;
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Rvalue references}
\begin{itemize}
\item alias which refers to an rvalue
\begin{lstlisting}
string&& sr = "Hello"; // create temporary
cout << sr;
\end{lstlisting}
\item used to implement move semantics and perfect forwarding
\item move temporaries into function instead of copying their values
\begin{lstlisting}
void sinkStr(string&& tmp) { cout << tmp; }
sinkStr("Hello World!");
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Pointers}
\begin{itemize}
\item pointers are data types that hold addresses as their values
\item can be dereferenced: interpret data at address as value
\item can be used for pointer arithmetics
\begin{lstlisting}
int x = 42;
int* px = &x; // px holds lvalue (address) of x
cout << px; // print address
*px = 43; // set content of variable at &x
cout << *px // print 43 (value of x)
struct { int x; } t, *pt = &t;
(*pt).x = 1; // dereference then access member
pt->x = 2; // preferred shortcut
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Pointer arithmetics}
\begin{itemize}
\item addresses are basically numbers
\item for T* offsets are relative to sizeof(T)
\item can be used for low level programming and arrays
\begin{lstlisting}
int arr[12]; // &arr[0] == 0xDEAD0000
int* p = arr; // p = &arr[0]
p + 1; // 0xDEAD004 with sizeof(int) = 4
p + 3; // 0xDEAD00C
*p = 1; // arr[0] = 1
*(p + 1) = 2; // arr[1] = 2
p[11] = 12; // arr[11] = 12
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Nullpointer}
\begin{itemize}
\item a pointer that holds address 0x00 is a nullpointer
\item C++ has special value called nullptr
\item \textbf{never use NULL or 0 instead of nullptr}
\item dereferencing nullptr is undefined behavior (usually segfault)
\item testing for nullptr is important
\begin{lstlisting}
int x = 12;
int* p = &x;
int* np = nullptr;
if (p != nullptr) { cout << "not null"; }
if (!np) { cout << "null"; }
if (p && *p) { cout << *p; }
if (np && *np) { cout << *np; } // no error.
\end{lstlisting}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Value semantics}
\begin{itemize}
\item programming style that focuses on values stored in objects
\item identity of objects is irrelevant
\item default behavior of C++
\item mathematical approach
\item easy to use
\item no reference aliasing problems: cannot implicitly modify a variable
\item can be highly optimized with move semantics, copy elision, etc.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Value semantics: example}
\begin{lstlisting}
struct Member { string s; };
struct Host { Member m; };
/* ... */
Host host;
Member member;
member.s = "Hello";
host.m = member;
cout << host.m.s; // print "Hello"
member.s = "World";
cout << host.m.s; // print "Hello"
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Reference semantics}
\begin{itemize}
\item programming style that focuses on object identity
\item can be accomplished in C++ with references and pointers
\item object-oriented approach (default behavior of Java)
\item allows different parties to modify objects
\item reference aliasing problems can occur
\item in OOP copying references is usually cheaper than copying values
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Reference semantics: example}
\begin{lstlisting}
struct Member { string s; };
struct Host { Member* m; };
/* ... */
Host host;
Member member;
member.s = "Hello";
host.m = &member;
cout << host.m->s; // print "Hello"
member.s = "World";
cout << host.m->s; // print "World"
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Call by value / reference}
\begin{lstlisting}
void val(int x) { ++x; }
void ref(int& x) { ++x; }
void ptr(int* x) { ++*x; }
/* ... */
int i = 0;
cout << i; // print 0
val(i);
cout << i; // print 0
ref(i);
cout << i; // print 1
ptr(&i);
cout << i; // print 2
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Namespaces}
\lstinputlisting{examples/01-hello-world.cpp}
\begin{lstlisting}
using namespace std;
namespace own {
void otherFunc() { return; }
}
own::otherFunc();
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Namespaces}
\begin{lstlisting}
void outsider() { return; }
namespace own {
void firstFunc() { return; }
namespace second {
void otherFunc() {
firstFunc(); ::outsider(); return;
}
}
}
own::second::otherFunc();
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Namespaces}
\begin{lstlisting}
int twice() { return 1; }
namespace own {
int twice() { return 2; }
void firstFunc() {
std::cout << own::twice();
std::cout << ::twice();
std::cout << twice();
}
}
inline namespace own {
void firstFunc() { return; }
}
firstFunc();
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Namespaces}
\begin{lstlisting}
#include <iostream>
using std::cout;
int main() {
cout << "Hello World!\n";
return 0;
}
\end{lstlisting}
\begin{lstlisting}
#include <iostream>
// using namespace std;
int main() {
std::cout << "Hello World!\n";
return 0;
}
\end{lstlisting}
\end{frame}
\end{document}