|
| 1 | +%Template |
| 2 | +%Copyright (C) 2019 Patrick Diehl |
| 3 | +% |
| 4 | +%This program is free software: you can redistribute it and/or modify |
| 5 | +%it under the terms of the GNU General Public License as published by |
| 6 | +%the Free Software Foundation, either version 3 of the License, or |
| 7 | +%(at your option) any later version. |
| 8 | + |
| 9 | +%This program is distributed in the hope that it will be useful, |
| 10 | +%but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +%GNU General Public License for more details. |
| 13 | + |
| 14 | +%You should have received a copy of the GNU General Public License |
| 15 | +%along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +\documentclass[12pt,t]{beamer} |
| 17 | + |
| 18 | +\beamertemplatenavigationsymbolsempty |
| 19 | + |
| 20 | +% usepackage |
| 21 | +%\usepackage{template/dbt} |
| 22 | +\usepackage{listings} |
| 23 | + |
| 24 | +\definecolor{comments}{RGB}{81,81,81} |
| 25 | +\definecolor{keywords}{RGB}{255,0,90} |
| 26 | + |
| 27 | +% lstlisting |
| 28 | +\lstset{ |
| 29 | + language=C, |
| 30 | + basicstyle=\footnotesize\ttfamily, |
| 31 | + keywordstyle=\color{keywords}, |
| 32 | + showspaces=false, |
| 33 | + showstringspaces=false, |
| 34 | + commentstyle=\color{blue}\emph |
| 35 | + %frame=single, |
| 36 | + %rulecolor=\color{comments}, |
| 37 | + %rulesepcolor=\color{comments}, |
| 38 | + %backgroundcolor = \color{lightgray} |
| 39 | +} |
| 40 | + |
| 41 | +\usetheme{default} |
| 42 | + |
| 43 | +\usepackage[ |
| 44 | + type={CC}, |
| 45 | + modifier={by-nc-nd}, |
| 46 | + version={4.0}, |
| 47 | +]{doclicense} |
| 48 | + |
| 49 | +\input{template/variables.tex} |
| 50 | + |
| 51 | +% frame slide |
| 52 | +\title{\coursename} |
| 53 | +\subtitle{Lecture 20: Managing memory and low-level data structures} |
| 54 | + |
| 55 | +%\author{\href{}{}} |
| 56 | +%\institute { |
| 57 | +% \href{}{\tt \scriptsize \today} |
| 58 | +%} |
| 59 | +\date { |
| 60 | + \tiny \url{\courseurl} |
| 61 | +\vspace{2cm} |
| 62 | +\doclicenseThis |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +\usepackage{ifxetex} |
| 69 | + |
| 70 | +\ifxetex |
| 71 | +\usepackage{fontspec} |
| 72 | +\setmainfont{Raleway} |
| 73 | +\fi |
| 74 | + |
| 75 | +\ifluatex |
| 76 | +\usepackage{fontspec} |
| 77 | +\setmainfont{Raleway} |
| 78 | +\fi |
| 79 | + |
| 80 | +\usepackage{tikz} |
| 81 | + |
| 82 | +\begin{document} { |
| 83 | + \setbeamertemplate{footline}{} |
| 84 | + \frame { |
| 85 | + \titlepage |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +\frame{ |
| 90 | + |
| 91 | +\tableofcontents |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +\AtBeginSection[]{ |
| 96 | + \begin{frame} |
| 97 | + \vfill |
| 98 | + \centering |
| 99 | + \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title} |
| 100 | + \usebeamerfont{title}\insertsectionhead\par% |
| 101 | + \end{beamercolorbox} |
| 102 | + \vfill |
| 103 | + \end{frame} |
| 104 | +} |
| 105 | + |
| 106 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 107 | +\section{Reminder} |
| 108 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 109 | +\begin{frame}{Lecture 19} |
| 110 | +\begin{block}{What you should know from last lecture} |
| 111 | +\begin{itemize} |
| 112 | +\item Running distributed HPX applications |
| 113 | +\item Using the slurm environment and modules on clusters |
| 114 | +\end{itemize} |
| 115 | +\end{block} |
| 116 | +\end{frame} |
| 117 | + |
| 118 | + |
| 119 | +\begin{frame}{Why do we talk about pointers so late?} |
| 120 | + |
| 121 | +\textbf{Person A}: Would you teach a toddler how to eat with a butcher's knife? \\ |
| 122 | +\vspace{1cm} |
| 123 | +\textbf{Person B}: No? \\ |
| 124 | +\vspace{1cm} |
| 125 | +\textbf{Person A}: So stop mentioning pointers to people barely starting with C++ |
| 126 | +\end{frame} |
| 127 | + |
| 128 | + |
| 129 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 130 | +\section{Pointer} |
| 131 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 132 | + |
| 133 | +\begin{frame}{Pointer} |
| 134 | + |
| 135 | +\begin{center} |
| 136 | +\begin{tikzpicture} |
| 137 | +\draw (0,0) rectangle ++(0.75,0.75) node[pos=.5] {$p$}; |
| 138 | +\draw (1.5,0) rectangle ++(0.75,0.75) node[pos=.5] {$x$}; |
| 139 | +\draw[->] (0.75,0.365) -- (1.5,0.365); |
| 140 | +\end{tikzpicture} |
| 141 | + |
| 142 | +\end{center} |
| 143 | +\begin{itemize} |
| 144 | +\item A pointer $p$ is a value that represents the address of an object |
| 145 | +\item Every object $x$ has a distinct unique address to a part of the computer's memory. |
| 146 | +\end{itemize} |
| 147 | + |
| 148 | +\begin{block}{Operators} |
| 149 | +\begin{itemize} |
| 150 | +\item The address of object $x$ can be accessed using the $\&$ address operator |
| 151 | +\item The deference operator * provides the object the pointer is pointing to |
| 152 | +\end{itemize} |
| 153 | +\end{block} |
| 154 | + |
| 155 | +\end{frame} |
| 156 | + |
| 157 | +\begin{frame}[fragile]{Example} |
| 158 | + |
| 159 | +\begin{lstlisting} |
| 160 | +// Initialize |
| 161 | +int x = 42; |
| 162 | + |
| 163 | +// Get the pointer to the object x |
| 164 | +int* p = &x; |
| 165 | + |
| 166 | +// Get the object the pointer is pointing to |
| 167 | +int tmp = *p; |
| 168 | + |
| 169 | +// Using pointers to manipulate objects |
| 170 | +std::cout << x << std::endl; |
| 171 | +*p = 42; |
| 172 | +std::cout << x << std::endl; |
| 173 | + |
| 174 | +\end{lstlisting} |
| 175 | + |
| 176 | +\end{frame} |
| 177 | + |
| 178 | +\begin{frame}[fragile]{Pointer arithmetic} |
| 179 | + |
| 180 | +\begin{lstlisting} |
| 181 | +int* array = new[3]; |
| 182 | +*array = 1; |
| 183 | +*(array + 1) = 2; |
| 184 | +*(array + 1) = 3; |
| 185 | + |
| 186 | +// Accessing the first element |
| 187 | +int first = *array; |
| 188 | + |
| 189 | +// Accessing the second element |
| 190 | +int second = *(array + 1); |
| 191 | + |
| 192 | +// Getting the distance between two pointers |
| 193 | +ptrdiff_t dist = array+2 - array; |
| 194 | +\end{lstlisting} |
| 195 | + |
| 196 | +Note that \lstinline|ptrdiff_t| is a signed type because the distance can be negative |
| 197 | +\end{frame} |
| 198 | + |
| 199 | + |
| 200 | +\begin{frame}[fragile]{Pointers to functions} |
| 201 | + |
| 202 | +\begin{lstlisting} |
| 203 | +int square(int a) |
| 204 | +{ |
| 205 | +return a * a; |
| 206 | +} |
| 207 | + |
| 208 | +// Generating a function pointer |
| 209 | +int (*fp)(int) = square; //We need the (int) for |
| 210 | +int (*fp2)(int) = □ // the return type |
| 211 | + |
| 212 | +// Calling the function using its pointer |
| 213 | +std::cout << (*fp)(5); |
| 214 | +std::cout << fp2(5); |
| 215 | + |
| 216 | +\end{lstlisting} |
| 217 | +Note that each of two lines to get the pointer or call the function are equivalent. |
| 218 | + |
| 219 | +\end{frame} |
| 220 | + |
| 221 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 222 | +\section{Arrays} |
| 223 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 224 | + |
| 225 | +\begin{frame}{Array vs Vector} |
| 226 | + |
| 227 | + |
| 228 | +\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}}} |
| 229 | +\begin{itemize} |
| 230 | +\item Language feature |
| 231 | +\item Number of elements must be known at compile time |
| 232 | +\item Can not grow or shrink dynamically |
| 233 | +\end{itemize} |
| 234 | +\end{block} |
| 235 | + |
| 236 | +\begin{block}{Vector} |
| 237 | +\begin{itemize} |
| 238 | +\item Part of the standard library |
| 239 | +\item Can grow or shrink dynamically |
| 240 | +\end{itemize} |
| 241 | +\end{block} |
| 242 | +An array is a kind of container, similar to a vector but less powerful. |
| 243 | +\end{frame} |
| 244 | + |
| 245 | +\begin{frame}[fragile]{Working with arrays} |
| 246 | + |
| 247 | +\begin{lstlisting} |
| 248 | +//Define the length |
| 249 | +size_t size = 6; |
| 250 | + |
| 251 | +//Generate a double arry of size 6 |
| 252 | +double array[size]; |
| 253 | + |
| 254 | +//Access all elements |
| 255 | +for(size_t i = 0; i < size ; i++){ |
| 256 | + array[i] = i; |
| 257 | + std::cout << array[i] << std::endl; |
| 258 | + } |
| 259 | + |
| 260 | +//Access the first element |
| 261 | +*array = 42; |
| 262 | +std::cout << array[0] << std::endl; |
| 263 | + |
| 264 | +//Initializing |
| 265 | +double array = {1,2,3.5,5}; |
| 266 | +\end{lstlisting} |
| 267 | + |
| 268 | +\end{frame} |
| 269 | + |
| 270 | + |
| 271 | + |
| 272 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 273 | +\section{Command line arguments} |
| 274 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 275 | + |
| 276 | +\begin{frame}[fragile]{Arguments to \lstinline|main|} |
| 277 | + |
| 278 | +\begin{lstlisting} |
| 279 | +int main(int argc, char** argv) |
| 280 | +{ |
| 281 | + |
| 282 | + return EXIT_SUCCESS: |
| 283 | +} |
| 284 | +\end{lstlisting} |
| 285 | + |
| 286 | +\begin{block}{Parameters} |
| 287 | +\begin{itemize} |
| 288 | +\item \lstinline|int argc| -- Number of pointers in the \lstinline|char** argv| |
| 289 | +\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. |
| 290 | +\end{itemize} |
| 291 | +\end{block} |
| 292 | +Note that these parameters can have any name, but the two presented are very common. |
| 293 | +\end{frame} |
| 294 | + |
| 295 | + |
| 296 | +\begin{frame}[fragile]{Example} |
| 297 | + |
| 298 | +\lstinputlisting{code/20/argument.cpp} |
| 299 | + |
| 300 | +\end{frame} |
| 301 | + |
| 302 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 303 | +\section{Memory management} |
| 304 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 305 | + |
| 306 | + |
| 307 | +\begin{frame}{Three kind of memory management} |
| 308 | + |
| 309 | +\begin{block}{Automatic memory management} |
| 310 | +\begin{itemize} |
| 311 | +\item What we have done so far |
| 312 | +\item The system is allocating the memory for a local variable |
| 313 | +\item The system is deallocating the memory if the variable goes out of scope |
| 314 | +\end{itemize} |
| 315 | +\end{block} |
| 316 | + |
| 317 | +\begin{block}{Dynamic memory management} |
| 318 | +\begin{itemize} |
| 319 | +\item The programmer allocates the memory with the the \lstinline|new|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/new}} keyword |
| 320 | +\item The programmer deallocates the memory with the the \lstinline|delete|\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/delete}} keyword |
| 321 | +\end{itemize} |
| 322 | +\end{block} |
| 323 | + |
| 324 | +\end{frame} |
| 325 | + |
| 326 | +\begin{frame}[fragile]{Memory management of an object} |
| 327 | + |
| 328 | +\begin{block}{Allocation} |
| 329 | +\begin{lstlisting} |
| 330 | +int* p = new int(42); |
| 331 | +\end{lstlisting} |
| 332 | +\end{block} |
| 333 | + |
| 334 | +\begin{block}{Deallocation} |
| 335 | +\begin{lstlisting} |
| 336 | +delete p; |
| 337 | +\end{lstlisting} |
| 338 | +\end{block} |
| 339 | + |
| 340 | + |
| 341 | +\end{frame} |
| 342 | + |
| 343 | +\begin{frame}[fragile]{Memory management of an array} |
| 344 | + |
| 345 | +\begin{block}{Allocation} |
| 346 | +\begin{lstlisting} |
| 347 | +int* p = new int[5]; |
| 348 | +\end{lstlisting} |
| 349 | +\end{block} |
| 350 | + |
| 351 | +\begin{block}{Deallocation} |
| 352 | +\begin{lstlisting} |
| 353 | +delete[] p; |
| 354 | +\end{lstlisting} |
| 355 | +\end{block} |
| 356 | + |
| 357 | + |
| 358 | +\end{frame} |
| 359 | + |
| 360 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 361 | +\section{Summary} |
| 362 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 363 | +\begin{frame}{Summary} |
| 364 | +\begin{block}{After this lecture, you should know} |
| 365 | +\begin{itemize} |
| 366 | +\item Pointer and Arrays |
| 367 | +\item How to read command line arguments |
| 368 | +\item Allocating and deallocating memory |
| 369 | +\end{itemize} |
| 370 | +\end{block} |
| 371 | +\end{frame} |
| 372 | + |
| 373 | + |
| 374 | +\end{document} |
0 commit comments