-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture20.tex
372 lines (271 loc) · 8.35 KB
/
lecture20.tex
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
%Template
%Copyright (C) 2019 Patrick Diehl
%
%This program is free software: you can redistribute it and/or modify
%it under the terms of the GNU General Public License as published by
%the Free Software Foundation, either version 3 of the License, or
%(at your option) any later version.
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%GNU General Public License for more details.
%You should have received a copy of the GNU General Public License
%along with this program. If not, see <http://www.gnu.org/licenses/>.
\providecommand\classoption{12pt}
\documentclass[\classoption]{beamer}
\input{./template/packages}
\input{template/variables.tex}
% frame slide
\title{\coursename}
\subtitle{Lecture 20: Managing memory and low-level data structures}
\author{\tiny Patrick Diehl \orcid{0000-0003-3922-8419}}
%\institute {
% \href{}{\tt \scriptsize \today}
%}
\date {
\tiny \url{\courseurl}
\vspace{2cm}
\doclicenseThis
}
\usepackage{ifxetex}
\ifxetex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\ifluatex
\usepackage{fontspec}
\setmainfont{Raleway}
\fi
\usepackage{tikz}
\begin{document} {
\setbeamertemplate{footline}{}
\frame {
\titlepage
}
}
\frame{
\tableofcontents
}
\AtBeginSection[]{
\begin{frame}
\vfill
\centering
\begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
\usebeamerfont{title}\insertsectionhead\par%
\end{beamercolorbox}
\vfill
\end{frame}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Reminder}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Lecture 19}
\begin{block}{What you should know from last lecture}
\begin{itemize}
\item Running distributed HPX applications
\item Using the slurm environment and modules on clusters
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Why do we talk about pointers so late?}
\textbf{Person A}: Would you teach a toddler how to eat with a butcher's knife? \\
\vspace{1cm}
\textbf{Person B}: No? \\
\vspace{1cm}
\textbf{Person A}: So stop mentioning pointers to people barely starting with C++
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Pointer}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Pointer}
\begin{center}
\begin{tikzpicture}
\draw (0,0) rectangle ++(0.75,0.75) node[pos=.5] {$p$};
\draw (1.5,0) rectangle ++(0.75,0.75) node[pos=.5] {$x$};
\draw[->] (0.75,0.365) -- (1.5,0.365);
\end{tikzpicture}
\end{center}
\begin{itemize}
\item A pointer $p$ is a value that represents the address of an object
\item Every object $x$ has a distinct unique address to a part of the computer's memory.
\end{itemize}
\begin{block}{Operators}
\begin{itemize}
\item The address of object $x$ can be accessed using the $\&$ address operator
\item The deference operator * provides the object the pointer is pointing to
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]{Example}
\begin{lstlisting}
// Initialize
int x = 42;
// Get the pointer to the object x
int* p = &x;
// Get the object the pointer is pointing to
int tmp = *p;
// Using pointers to manipulate objects
std::cout << x << std::endl;
*p = 43;
std::cout << x << std::endl;
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Pointer arithmetic}
\begin{lstlisting}
int* array = new int[3];
*array = 1;
*(array + 1) = 2;
*(array + 2) = 3;
// Accessing the first element
int first = *array;
// Accessing the second element
int second = *(array + 1);
// Getting the distance between two pointers
ptrdiff_t dist = array+2 - array;
\end{lstlisting}
Note that \lstinline|ptrdiff_t| is a signed type because the distance can be negative
\end{frame}
\begin{frame}[fragile]{Pointers to functions}
\begin{lstlisting}
int square(int a)
{
return a * a;
}
// Generating a function pointer
int (*fp)(int) = square; //We need the (int) for
int (*fp2)(int) = □ // the return type
// Calling the function using its pointer
std::cout << (*fp)(5);
std::cout << fp2(5);
\end{lstlisting}
Note that each of two lines to get the pointer or call the function are equivalent.
\end{frame}
\begin{frame}[fragile]{Pointer to classes and structures}
\begin{lstlisting}
struct A {
public:
A(double in)
{
value = in;
}
double value;
};
A* a = new A(20);
double val = a->value;
\end{lstlisting}
Note that we use \lstinline|->| expression to access the variable of a struct \lstinline|a|
using its pointer.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Arrays}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Array vs Vector}
\begin{block}{Array\footnote{\tiny\url{https://en.cppreference.com/w/cpp/container/array}}\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/array}}}
\begin{itemize}
\item Language feature
\item Number of elements must be known at compile time
\item Can not grow or shrink dynamically
\end{itemize}
\end{block}
\begin{block}{Vector}
\begin{itemize}
\item Part of the standard library
\item Can grow or shrink dynamically
\end{itemize}
\end{block}
An array is a kind of container, similar to a vector but less powerful.
\end{frame}
\begin{frame}[fragile]{Working with arrays}
\begin{lstlisting}
//Define the length
size_t size = 6;
//Generate a double array of size 6
double array[size];
//Access all elements
for(size_t i = 0; i < size ; i++){
array[i] = i;
std::cout << array[i] << std::endl;
}
//Access the first element
*array = 42;
std::cout << array[0] << std::endl;
//Initializing
double array = {1,2,3.5,5};
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Command line arguments}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Arguments to \lstinline|main|}
\begin{lstlisting}
int main(int argc, char** argv)
{
return EXIT_SUCCESS:
}
\end{lstlisting}
\begin{block}{Parameters}
\begin{itemize}
\item \lstinline|int argc| -- Number of pointers in the \lstinline|char** argv|
\item \lstinline|char** argv| -- Initial pointer to an array of pointers for each command line option. Note that the first entry is the name of the executable.
\end{itemize}
\end{block}
Note that these parameters can have any name, but the two presented are very common.
\end{frame}
\begin{frame}[fragile]{Example}
\lstinputlisting{cpp/lecture20-argument.cpp}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Memory management}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Two kind of memory management}
\begin{block}{Automatic memory management}
\begin{itemize}
\item What we have done so far
\item The system is allocating the memory for a local variable
\item The system is deallocating the memory if the variable goes out of scope
\end{itemize}
\end{block}
\begin{block}{Dynamic memory management}
\begin{itemize}
\item The programmer allocates the memory with the \lstinline|new|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/new}} keyword
\item The programmer deallocates the memory with the \lstinline|delete|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/delete}} keyword
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]{Memory management of an object}
\begin{block}{Allocation}
\begin{lstlisting}
int* p = new int(42);
\end{lstlisting}
\end{block}
\begin{block}{Deallocation}
\begin{lstlisting}
delete p;
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{Memory management of an array}
\begin{block}{Allocation}
\begin{lstlisting}
int* p = new int[5];
\end{lstlisting}
\end{block}
\begin{block}{Deallocation}
\begin{lstlisting}
delete[] p;
\end{lstlisting}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Summary}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Summary}
\begin{block}{After this lecture, you should know}
\begin{itemize}
\item Pointer and Arrays
\item How to read command line arguments
\item Allocating and deallocating memory
\end{itemize}
\end{block}
\end{frame}
\end{document}