diff --git a/20200109/hp-2019ws-p4.tex b/20200109/hp-2019ws-p4.tex index 3f4a14e0d8fc82d0b78e65837211f62cbf8f41b6..0091478169680e3c97a7d149a80ef7064f6ae9ab 100644 --- a/20200109/hp-2019ws-p4.tex +++ b/20200109/hp-2019ws-p4.tex @@ -20,7 +20,7 @@ % Attribution-ShareAlike 3.0 Unported License along with this % document. If not, see <http://creativecommons.org/licenses/>. -% README: Versuch 16. und 23.1.2020: Objektorientiertes Grafik-Programm +% README: Versuch 4, 16. und 23.1.2020: Objektorientiertes Grafik-Programm \documentclass[a4paper]{article} diff --git a/20200116/Tower_of_Hanoi.jpeg b/20200116/Tower_of_Hanoi.jpeg new file mode 120000 index 0000000000000000000000000000000000000000..a1a794afda08596ffa2f46f278db53455de25b6c --- /dev/null +++ b/20200116/Tower_of_Hanoi.jpeg @@ -0,0 +1 @@ +../common/Tower_of_Hanoi.jpeg \ No newline at end of file diff --git a/20200116/dynmem-1.c b/20200116/dynmem-1.c new file mode 100644 index 0000000000000000000000000000000000000000..56148108873fc8dae9bdb7f8b555a4172883c6ad --- /dev/null +++ b/20200116/dynmem-1.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include <stdlib.h> + +int main (void) +{ + char **name = malloc (3 * sizeof (char *)); + name[0] = "Anton"; + name[1] = "Berthold"; + name[2] = "Caesar"; + for (int i = 0; i < 3; i++) + printf ("%s\n", name[i]); + free (name); + return 0; +} diff --git a/20200116/dynmem-2.c b/20200116/dynmem-2.c new file mode 100644 index 0000000000000000000000000000000000000000..f78a21ec7a8285f4c3fc41e3b549dd333e11c22e --- /dev/null +++ b/20200116/dynmem-2.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdlib.h> + +int main (void) +{ + char **name = malloc (3 * sizeof (char *)); + name[0] = "Anton"; + name[1] = "Berthold"; + name[2] = "Caesar"; + + char **new_name = malloc (4 * sizeof (char*)); + for (int i = 0; i < 3; i++) + new_name[i] = name[i]; + name = new_name; /* Speicherleck: Array wurde nicht freigegeben! */ + + name[3] = "Dieter"; + + for (int i = 0; i < 4; i++) + printf ("%s\n", name[i]); + free (name); + return 0; +} diff --git a/20200116/dynmem-3.c b/20200116/dynmem-3.c new file mode 100644 index 0000000000000000000000000000000000000000..0db6dd4f7f67d8d4db8ce8264a0ba533082da721 --- /dev/null +++ b/20200116/dynmem-3.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdlib.h> + +int main (void) +{ + char **name = malloc (3 * sizeof (char *)); + name[0] = "Anton"; + name[1] = "Berthold"; + name[2] = "Caesar"; + + char **new_name = malloc (4 * sizeof (char*)); + for (int i = 0; i < 3; i++) + new_name[i] = name[i]; + free (name); + name = new_name; /* kein Speicherleck: Array wurde freigegeben. */ + + name[3] = "Dieter"; + + for (int i = 0; i < 4; i++) + printf ("%s\n", name[i]); + free (name); + return 0; +} diff --git a/20200116/dynmem-4.c b/20200116/dynmem-4.c new file mode 100644 index 0000000000000000000000000000000000000000..d1ebf99fd76b60aa3bcf97ebc2eb7b76c1a583a9 --- /dev/null +++ b/20200116/dynmem-4.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> + +int main (void) +{ + char **name = malloc (3 * sizeof (char *)); + name[0] = "Anton"; + name[1] = "Berthold"; + name[2] = "Caesar"; + + char **new_name = malloc (4 * sizeof (char*)); + for (int i = 0; i < 3; i++) + new_name[i] = name[i]; + free (name); + name = new_name; + + name[3] = "Dieter"; + + for (int i = 0; i < 4; i++) + printf ("%s\n", name[i]); + free (name); + free (new_name); /* Fehler: Array wurde bereits freigegeben! */ + return 0; +} diff --git a/20200116/dynmem-5.c b/20200116/dynmem-5.c new file mode 100644 index 0000000000000000000000000000000000000000..c8a5807d1172df17419fa05efffcb643b8e75ba7 --- /dev/null +++ b/20200116/dynmem-5.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> + +int main (void) +{ + char **name = malloc (3 * sizeof (char *)); + name[0] = "Anton"; + name[1] = "Berthold"; + name[2] = "Caesar"; + + char **new_name = malloc (4 * sizeof (char*)); + for (int i = 0; i < 3; i++) + new_name[i] = name[i]; + free (name); + name = new_name; + new_name = NULL; /* Zeiger wird nicht mehr gebraucht. */ + + name[3] = "Dieter"; + + for (int i = 0; i < 4; i++) + printf ("%s\n", name[i]); + free (name); + return 0; +} diff --git a/20200116/dynmem-6.c b/20200116/dynmem-6.c new file mode 100644 index 0000000000000000000000000000000000000000..b80cd7fa18551d2046a2aa77e7f4d1bc74157f0b --- /dev/null +++ b/20200116/dynmem-6.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdlib.h> + +int main (void) +{ + char **name = malloc (3 * sizeof (char *)); + name[0] = "Anton"; + name[1] = "Berthold"; + name[2] = "Caesar"; + name = realloc (name, 4 * sizeof (char)); + name[3] = "Dieter"; /* Fehler: ^ "*" vergessen; zu wenig Speicher */ + for (int i = 0; i < 4; i++) + printf ("%s\n", name[i]); + free (name); + return 0; +} diff --git a/20200116/dynmem-7.c b/20200116/dynmem-7.c new file mode 100644 index 0000000000000000000000000000000000000000..414a24a14321085196c5c2887f3c7d90e7c81e01 --- /dev/null +++ b/20200116/dynmem-7.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdlib.h> + +int main (void) +{ + char **name = malloc (3 * sizeof (char *)); + name[0] = "Anton"; + name[1] = "Berthold"; + name[2] = "Caesar"; + name = realloc (name, 4 * sizeof (char *)); + name[3] = "Dieter"; + for (int i = 0; i < 4; i++) + printf ("%s\n", name[i]); + free (name); + return 0; +} diff --git a/20200116/hello-gtk.png b/20200116/hello-gtk.png new file mode 120000 index 0000000000000000000000000000000000000000..cca99209d86683a9a3b0f70bbc149780bae10ba6 --- /dev/null +++ b/20200116/hello-gtk.png @@ -0,0 +1 @@ +../common/hello-gtk.png \ No newline at end of file diff --git a/20200116/hp-20200116.pdf b/20200116/hp-20200116.pdf new file mode 100644 index 0000000000000000000000000000000000000000..da6d761d3af2e5e06b73867c862801c0a85d22d6 Binary files /dev/null and b/20200116/hp-20200116.pdf differ diff --git a/20200116/hp-20200116.tex b/20200116/hp-20200116.tex new file mode 100644 index 0000000000000000000000000000000000000000..b0ec7c7d5a3c3b87d8e0b69ef63561ced0896274 --- /dev/null +++ b/20200116/hp-20200116.tex @@ -0,0 +1,935 @@ +% hp-20200116.pdf - Lecture Slides on Low-Level Programming +% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018, 2019, 2020 Peter Gerwinski +% +% This document is free software: you can redistribute it and/or +% modify it either under the terms of the Creative Commons +% Attribution-ShareAlike 3.0 License, or 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 document 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 document. If not, see <http://www.gnu.org/licenses/>. +% +% You should have received a copy of the Creative Commons +% Attribution-ShareAlike 3.0 Unported License along with this +% document. If not, see <http://creativecommons.org/licenses/>. + +% README: objektorientierte Programmierung, dynamische Speicherverwaltung + +\documentclass[10pt,t]{beamer} + +\usepackage{pgslides} +\usepackage{tikz} +\usepackage{rotating} + +\usefonttheme[onlymath]{serif} + +\definecolor{medcyan}{rgb}{0.0,0.5,0.7} + +\title{Hardwarenahe Programmierung} +\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski} +\date{16.\ Januar 2020} + +\begin{document} + +\maketitleframe + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}} + \item[\textbf{2}] \textbf{Einführung in C} + \item[\textbf{3}] \textbf{Bibliotheken} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \item[\textbf{5}] \textbf{Algorithmen} + \begin{itemize} + \item[5.1] Differentialgleichungen + \color{medcyan} + \item[5.\hbox to 0.56em{\boldmath$\frac{1+i}{\sqrt2}$}]\hspace*{0.90em}Quantencomputer + \vspace*{1pt} + \color{medgreen} + \item[5.2] Rekursion + \item[5.3] Aufwandsabschätzungen + \end{itemize} + \item[\textbf{6}] \textbf{Objektorientierte Programmierung} + \begin{itemize} + \color{red} + \item[6.0] Dynamische Speicherverwaltung + \color{medgreen} + \item[6.1] Konzepte und Ziele + \item[6.2] Beispiel: Zahlen und Buchstaben + \color{red} + \item[6.3] Unions + \item[6.4] Virtuelle Methoden + \item[6.5] Beispiel: Graphische Benutzeroberfläche (GUI) + \item[6.6] Ausblick: C++ + \end{itemize} + \item[\textbf{7}] \textbf{Datenstrukturen} + \end{itemize} + +\end{frame} + +\setcounter{section}{4} +\section{Algorithmen} +\setcounter{subsection}{1} +\subsection{Rekursion} + +\begin{frame}[fragile] + + \showsubsection + + Vollständige Induktion: + \vspace*{-0.725cm} + \begin{displaymath} + \hspace*{4cm} + \left. + \begin{array}{r} + \mbox{Aussage gilt für $n = 1$}\\[2pt] + \mbox{Schluß von $n - 1$ auf $n$} + \end{array} + \right\} + \mbox{Aussage gilt für alle $n\in\mathbb{N}$} + \end{displaymath} + \vspace*{-0.5cm} + + \pause + + Türme von Hanoi + + \begin{onlyenv}<2> + \begin{center} + \includegraphics[width=12.2cm]{Tower_of_Hanoi.jpeg} + \end{center} + \end{onlyenv} + + \begin{onlyenv}<3-> + \begin{itemize} + \item + 64 Scheiben, 3 Plätze, + \only<3-4>{\hfill\makebox(0,0)[rt]{\includegraphics[width=6cm]{Tower_of_Hanoi.jpeg}}}\\ + immer 1 Scheibe verschieben + \item + Ziel: Turm verschieben + \item + Es dürfen nur kleinere Scheiben\\ + auf größeren liegen. + \bigskip + \pause + \pause + \item + $n = 1$ Scheibe: fertig + \item + Wenn $n - 1$ Scheiben verschiebbar:\\ + schiebe $n - 1$ Scheiben auf Hilfsplatz,\\ + verschiebe die darunterliegende,\\ + hole $n - 1$ Scheiben von Hilfsplatz + \end{itemize} + \begin{onlyenv}<5> + \vspace{-4.3cm} + \begin{lstlisting}[gobble=8,xleftmargin=6.4cm] + void move (int from, int to, int disks) + { + if (disks == 1) + move_one_disk (from, to); + else + { + int help = 0 + 1 + 2 - from - to; + move (from, help, disks - 1); + move (from, to, 1); + move (help, to, disks - 1); + } + } + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<6-> + \vspace{-5.0cm} + \hspace*{7.4cm}\begin{minipage}[t]{5cm} + 32 Scheiben: + \begin{lstlisting}[gobble=10,style=terminal] + $ ¡time ./hanoi-9a¿ + ... + real 0m32,712s + user 0m32,708s + sys 0m0,000s + \end{lstlisting} + \pause[7] + \begin{itemize} + \arrowitem + etwas über 1 Minute\\ + für 64 Scheiben + \end{itemize} + \pause + \vspace*{-0.5cm} + \begin{picture}(0,0) + \color{red} + \put(0,0){\makebox(0,0)[bl]{\tikz[line width=1pt]{\draw(0,0)--(4,0.8);}}} + \put(0,0.8){\makebox(0,0)[tl]{\tikz[line width=1pt]{\draw(0,0)--(4,-0.8);}}} + \end{picture} + + Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit! + % 32.712 * 2^32 / 3600 / 24 / 365.25 = 4452.08032888280477602859 + \begin{itemize} + \arrowitem + $\frac{32,712\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4452$ + Jahre\\[\smallskipamount] + für 64 Scheiben + \end{itemize} + \end{minipage} + \end{onlyenv} + \end{onlyenv} + +\end{frame} + +\subsection{Aufwandsabschätzungen \protect\color{gray}-- Komplexitätsanalyse} + +\begin{frame}[fragile] + +% \newcommand{\w}{\hspace*{0.75pt}} + + \showsubsection + + \begin{picture}(0,0) + \put(7.6,-0.5){% + \begin{minipage}[t]{5.3cm} +% \vspace*{-1.0cm}\includegraphics{landau-symbols.pdf} + \vspace*{-1.0cm}\alt<6->{\includegraphics{landau-symbols-3.pdf}}% + {\alt<5->{\includegraphics{landau-symbols-2.pdf}}% + {\includegraphics{landau-symbols.pdf}}} + \small + \begin{description}\itemsep0pt\leftskip-0.5cm + \item[$n$:] Eingabedaten + \item[$g(n)$:] Rechenzeit + \end{description} + \end{minipage}} + \end{picture} + + \vspace*{-\bigskipamount} + + Wann ist ein Programm "`schnell"'? + + \medskip + + \begin{onlyenv}<1-2> + Türme von Hanoi: $\mathcal{O}(2^n)$ + \par\medskip + Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit! + % 32.712 * 2^32 / 3600 / 24 / 365.25 = 4452.08032888280477602859 + \begin{itemize} + \arrowitem + $\frac{32,712\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4452$ + Jahre\\[\smallskipamount] + für 64 Scheiben + \end{itemize} + + \bigskip + \end{onlyenv} + + \begin{onlyenv}<2-> + Faustregel:\\Schachtelung der Schleifen zählen\\ + $k$ Schleifen ineinander \textarrow\ $\mathcal{O}(n^k)$ + + \bigskip + \end{onlyenv} + + \begin{onlyenv}<3> + \textbf{Beispiel: Sortieralgorithmen} + + \smallskip + + Anzahl der Vergleiche bei $n$ Strings + \begin{itemize} + \item + Maximum suchen mit Schummeln: $\mathcal{O}(1)$ + \item + Maximum suchen: $\mathcal{O}(n)$ + \item + Selection-Sort: $\mathcal{O}(n^2)$ + \item + Bubble-Sort: $\mathcal{O}(n)$ bis $\mathcal{O}(n^2)$ + \item + Quicksort: $\mathcal{O}(n\log n)$ bis $\mathcal{O}(n^2)$ + \end{itemize} + + \end{onlyenv} + + \begin{onlyenv}<4> + \textbf{Wie schnell ist RSA-Verschlüsselung?} + + \smallskip + + \begin{math} + c = m^e\,\%\,N + \end{math} + \quad + ("`$\%$"' = "`modulo"') + + \medskip + + \begin{lstlisting}[gobble=6,xleftmargin=2em] + int c = 1; + for (int i = 0; i < e; i++) + c = (c * m) % N; + \end{lstlisting} + + \smallskip + + \begin{itemize} + \item + $\mathcal{O}(e)$ Iterationen +% \item +% wenn $n$ die Anzahl der Binärziffern (Bits) von $e$ ist: +% $\mathcal{O}(2^n)$ Iterationen + \item + mit Trick: + $\mathcal{O}(\log e)$ Iterationen ($\log e$ = Anzahl der Ziffern von $e$) + \end{itemize} + + \smallskip + + Jede Iteration enthält eine Multiplikation und eine Division.\\ + Aufwand dafür: $\mathcal{O}(\log e)$\\ + \textarrow\ Gesamtaufwand: $\mathcal{O}\bigl((\log e)^2\bigr)$ + + \end{onlyenv} + + \begin{onlyenv}<5-> + + \textbf{Wie schnell ist RSA?}\\ + + \smallskip + + ($n$ = typische beteiligte Zahl, z.\,B. $e,p,q$) + + \begin{itemize} + \item + Ver- und Entschlüsselung (Exponentiation):\\ + \strut\hbox to 3.5cm{\color{red}$\mathcal{O}\!\left((\log n)^2\right)$\hss} + \only<6->{{\color{magenta}$\mathcal{O}(n^2)$}} + \item + Schlüsselerzeugung (Berechnung von $d$):\\ + \strut\hbox to 3.5cm{\color{red}$\mathcal{O}\!\left((\log n)^2\right)$\hss} + \only<6->{{\color{magenta}$\mathcal{O}(n^2)$}} + \item + Verschlüsselung brechen (Primfaktorzerlegung):\\ + \strut\hbox to 3.5cm{\color{red}$\mathcal{O}\bigl(2^{\sqrt{\log n\,\cdot\,\log\log n}}\bigr)$\hss} + \only<6->{{\color{magenta}$\mathcal{O}\bigl(2^{\sqrt{n\log n}}\bigr)$}} + \end{itemize} + + \vspace{0cm plus 1filll} + + \textbf{Die Sicherheit von RSA beruht darauf, + daß das Brechen der Verschlüsselung aufwendiger ist als + \boldmath$\mathcal{O}\bigl((\log n)^k\bigr)$ (für beliebiges $k$).} + + \vspace*{0.65cm} + + \end{onlyenv} + +\end{frame} + +\section{Objektorientierte Programmierung} + +\addtocounter{subsection}{-1} +\subsection{Dynamische Speicherverwaltung} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \begin{itemize} + \item + Array: feste Anzahl von Elementen desselben Typs (z.\,B.\ 3 ganze Zahlen) + \item + Dynamisches Array: variable Anzahl von Elementen desselben Typs + \end{itemize} + + \bigskip + + \begin{lstlisting} + char *name[] = { "Anna", "Berthold", "Caesar" }; + + ... + + name[3] = "Dieter"; + \end{lstlisting} + + \begin{picture}(0,0) + \color{red} + \put(0,0){\line(3,1){3.5}} + \put(0,1){\line(3,-1){3.5}} + \end{picture} + +\end{frame} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \bigskip + + \begin{lstlisting} + #include <stdlib.h> + + ... + + char **name = malloc (3 * sizeof (char *)); + /* Speicherplatz für 3 Zeiger anfordern */ + + ... + + free (name) + /* Speicherplatz freigeben */ + + \end{lstlisting} + +\end{frame} + +\subsection{Konzepte und Ziele} + +\begin{frame} + + \showsection + \showsubsection + + \begin{itemize} + \item +% Array: feste Anzahl von Elementen desselben Typs (z.\,B.\ 3 ganze Zahlen) + Array: Elemente desselben Typs (z.\,B.\ 3 ganze Zahlen) +% \item +% Dynamisches Array: variable Anzahl von Elementen desselben Typs + \item + Problem: Elemente unterschiedlichen Typs + \item + Lösung: den Typ des Elements zusätzlich speichern + \end{itemize} + \begin{itemize} + \item + Funktionen, die mit dem Objekt arbeiten: \newterm{Methoden} + \begin{onlyenv}<1> + \item + Was die Funktion bewirkt,\\ + hängt vom Typ des Objekts ab + \item + Realisierung über endlose \lstinline{if}-Ketten + \end{onlyenv} + \begin{onlyenv}<2> + \item + Was die Funktion bewirkt + \begin{picture}(0,0) + \color{red} + \put(-4.00,-0.10){\tikz{\draw[thick](0,0.25)--(3.75,-0.05);% + \draw[thick](-0.1,-0.05)--(3.75,0.3);}} + \end{picture}% + Welche Funktion aufgerufen wird,\\ + hängt vom Typ des Objekts ab: \newterm{virtuelle Methode} + \item + Realisierung über endlose \lstinline{if}-Ketten% + \begin{picture}(0,0) + \color{red} + \put(-2.75,-0.10){\tikz{\draw[thick](0,0.25)--(2.5,-0.05);% + \draw[thick](-0.1,-0.05)--(2.5,0.3);}} + \put(1.5,-1.1){\begin{rotate}{7}\large\bf\textarrow\ + kommt gleich +% nächste Woche + \end{rotate}} + \end{picture} + Zeiger, die im Objekt gespeichert sind\\ + (Genaugenommen: Tabelle von Zeigern) + \end{onlyenv} + \end{itemize} + +\end{frame} + +\begin{frame} + + \showsection + \showsubsection + + \begin{itemize} + \item + Problem: Elemente unterschiedlichen Typs + \item + Lösung: den Typ des Elements zusätzlich speichern + \item + \newterm{Methoden\/} und \newterm{virtuelle Methoden} + \end{itemize} + + \begin{itemize} + \item + Zeiger auf verschiedene Strukturen\\ + mit einem gemeinsamen Anteil von Datenfeldern\\ + \textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassen} von Objekten + \item + Struktur, die \emph{nur\/} den gemeinsamen Anteil enthält\\ + \textarrow\ "`Vorfahr"', \newterm{Basisklasse}, \newterm{Vererbung} + \item +% Explizite Typumwandlung eines Zeigers auf die Basisklasse\\ +% in einen Zeiger auf die \newterm{abgeleitete Klasse}\\ +% \textarrow\ Man kann ein Array unterschiedlicher Objekte\\ +% \strut\phantom{\textarrow} in einer Schleife abarbeiten.\\ + Zeiger auf die Basisklasse dürfen auf Objekte\\ + der \newterm{abgeleiteten Klasse} zeigen\\ + \textarrow\ \newterm{Polymorphie} + \end{itemize} + +\end{frame} + +\subsection{Beispiel: Zahlen und Buchstaben} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + } t_base;¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\begin{frame}[fragile] + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + } t_base;¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage}\\[0.7cm] + \begin{onlyenv}<1> + \begin{minipage}{8cm} + \begin{lstlisting}[gobble=10] + ¡t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s };¿ + \end{lstlisting} + \end{minipage}% + \begin{picture}(0,0) + \color{red} + \put(-5.4,-0.8){\mbox{$\underbrace{\rule{1.45cm}{0pt}}_{\shortstack{\strut explizite\\Typumwandlung}}$}} + \end{picture} + \end{onlyenv} +% \begin{onlyenv}<2> +% \begin{minipage}{5cm} +% \begin{lstlisting}[gobble=10] +% ¡typedef union +% { +% t_base base; +% t_integer integer; +% t_string string; +% } t_object;¿ +% \end{lstlisting} % \end{minipage} +% \end{onlyenv} + \end{center} + +\end{frame} + +\subsection{Unions} + +\begin{frame}[fragile] + + \showsubsection + + Variable teilen sich denselben Speicherplatz. + + \medskip + + \begin{minipage}[t]{3.7cm} + \begin{lstlisting}[gobble=6] + ¡typedef union + { + int8_t i; + uint8_t u; + } num8_t;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{4.5cm} + \begin{lstlisting}[gobble=6] + ¡int main (void) + { + num8_t test; + test.i = -1; + printf ("%d\n", test.u); + return 0; + }¿ + \end{lstlisting} + \end{minipage} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Variable teilen sich denselben Speicherplatz. + + \medskip + + \begin{minipage}[t]{3.7cm} + \begin{lstlisting}[gobble=6] + ¡typedef union + { + char s[8]; + uint64_t x; + } num_char_t;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{4.5cm} + \begin{lstlisting}[gobble=6] + ¡int main (void) + { + num_char_t test = { "Hello!" }; + printf ("%lx\n", test.x); + return 0; + }¿ + \end{lstlisting} + \end{minipage} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Variable teilen sich denselben Speicherplatz. + + \medskip + + \begin{minipage}[t]{3.7cm} + \begin{lstlisting}[gobble=6] + ¡typedef union + { + t_base base; + t_integer integer; + t_string string; + } t_object;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{3.0cm} + \begin{lstlisting}[gobble=6] + + ¡typedef struct + { + int type; + } t_base;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{3.0cm} + \begin{lstlisting}[gobble=6] + + ¡typedef struct + { + int type; + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{3.0cm} + \begin{lstlisting}[gobble=6] + + ¡typedef struct + { + int type; + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage} + + \bigskip + + \begin{center} + \begin{minipage}{8.5cm} + \begin{lstlisting}[gobble=8] + ¡if (this->base.type == T_INTEGER) + printf ("Integer: %d\n", this->integer.content); + else if (this->base.type == T_STRING) + printf ("String: \"%s\"\n", this->string.content);¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\subsection{Virtuelle Methoden} + +\begin{frame}[fragile] + \showsubsection + + \begin{lstlisting} + void print_object (t_object *this) + { + if (this->base.type == T_INTEGER) + printf ("Integer: %d\n", this->integer.content); + else if (this->base.type == T_STRING) + printf ("String: \"%s\"\n", this->string.content); + } + \end{lstlisting} + + \begin{picture}(0,0) + \color{red} + \put(9,1.7){\shortstack[l]{if-Kette:\\\strut wird unübersichtlich}} + \put(1,-2){\mbox{\textarrow}} + \put(0,-3){\mbox{Zeiger auf Funktionen}} + \end{picture} + + \begin{lstlisting}[xleftmargin=4cm] + void print_integer (t_object *this) + { + printf ("Integer: %d\n", this->integer.content); + } + + void print_string (t_object *this) + { + printf ("String: \"%s\"\n", this->string.content); + } + \end{lstlisting} + +\end{frame} + +\begin{frame}[fragile] + \showsubsection + + Zeiger auf Funktionen + + \medskip + + \begin{lstlisting} + void (* print) (t_object *this); + \end{lstlisting} + \begin{picture}(0,1.2)(0,-0.9) + \color{red} + \put(0.95,0.3){\mbox{$\underbrace{\rule{1cm}{0pt}}$}} + \put(0.2,-0.7){\shortstack{das, worauf print zeigt,\\ist eine Funktion}} + \end{picture} + + \begin{itemize} + \item + Objekt enthält Zeiger auf Funktion + \begin{onlyenv}<1> + \medskip + \begin{lstlisting}[gobble=10] + typedef struct + { + void (* print) (union t_object *this); + int content; + } t_integer; + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<2-> + \vspace*{-3.5cm} % Why doesn't a picture environment work here??? :-( + \begin{lstlisting}[gobble=10,xleftmargin=5.5cm] + typedef struct + { + void (* print) (union t_object *this); + int content; + } t_integer; + \end{lstlisting} + \vspace*{0.85cm} + \bigskip + \smallskip + \end{onlyenv} + \pause + \item + Konstruktor initialisiert diesen Zeiger + \begin{onlyenv}<2> + \medskip + \begin{lstlisting}[gobble=10] + t_object *new_integer (int i) + { + t_object *p = malloc (sizeof (t_integer)); + p->integer.print = print_integer; + p->integer.content = i; + return p; + } + \end{lstlisting} + \vspace*{-2cm} + \end{onlyenv} + \pause + \item + Aufruf: "`automatisch"' die richtige Funktion + \begin{onlyenv}<3> + \medskip + \begin{lstlisting}[gobble=10] + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + \end{lstlisting} + \end{onlyenv} + \pause + \medskip + \item + in größeren Projekten:\\ + Objekt enthält Zeiger auf Tabelle von Funktionen + \end{itemize} +\end{frame} + +\subsection{Beispiel: Graphische Benutzeroberfläche (GUI)} + +\begin{frame}[fragile] + + \showsubsection + + \scriptsize + \begin{lstlisting} + #include <gtk/gtk.h> + + int main (int argc, char **argv) + { + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (window), vbox); + gtk_container_set_border_width (GTK_CONTAINER (vbox), 10); + GtkWidget *label = gtk_label_new ("Hello, world!"); + gtk_container_add (GTK_CONTAINER (vbox), label); + GtkWidget *button = gtk_button_new_with_label ("Quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + gtk_container_add (GTK_CONTAINER (vbox), button); + gtk_widget_show (button); + gtk_widget_show (label); + gtk_widget_show (vbox); + gtk_widget_show (window); + gtk_main (); + return 0; + } + \end{lstlisting} + + \vspace*{-6cm}\strut\hfill + \includegraphics[scale=0.85]{hello-gtk.png}\\[2cm] + \begin{flushright} + \normalsize\bf Praktikumsversuch:\\ + Objektorientiertes Zeichenprogramm + \end{flushright} + +\end{frame} + +\subsection{Ausblick: C++} + +\begin{frame}[fragile] + \showsubsection + + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + void (* print) (union t_object *this); + } t_base;¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5.5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + void (* print) (...); + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + void (* print) (union t_object *this); + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\begin{frame}[fragile] + \showsubsection + + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡struct TBase + { + virtual void print (void); + };¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5.5cm} + \begin{lstlisting}[gobble=8] + ¡struct TInteger: public TBase + { + virtual void print (void); + int content; + };¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡struct TString: public TBase + { + virtual void print (void); + char *content; + };¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\end{document} diff --git a/20200116/landau-symbols-2.pdf b/20200116/landau-symbols-2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..6b458b6efd8e274824a6dfcaabc4b9c27d196dc4 --- /dev/null +++ b/20200116/landau-symbols-2.pdf @@ -0,0 +1 @@ +../common/landau-symbols-2.pdf \ No newline at end of file diff --git a/20200116/landau-symbols-3.pdf b/20200116/landau-symbols-3.pdf new file mode 120000 index 0000000000000000000000000000000000000000..46efa409b35ff5df763c744a423599cba515d886 --- /dev/null +++ b/20200116/landau-symbols-3.pdf @@ -0,0 +1 @@ +../common/landau-symbols-3.pdf \ No newline at end of file diff --git a/20200116/landau-symbols.pdf b/20200116/landau-symbols.pdf new file mode 120000 index 0000000000000000000000000000000000000000..ca145425bf07439c680632aa0663f84be601a565 --- /dev/null +++ b/20200116/landau-symbols.pdf @@ -0,0 +1 @@ +../common/landau-symbols.pdf \ No newline at end of file diff --git a/20200116/objects-1.c b/20200116/objects-1.c new file mode 100644 index 0000000000000000000000000000000000000000..1a628668fd7b8fb3a0d9886ac14e8e909bc23793 --- /dev/null +++ b/20200116/objects-1.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { &i, &s }; + + return 0; +} diff --git a/20200116/objects-10.c b/20200116/objects-10.c new file mode 100644 index 0000000000000000000000000000000000000000..ac47c0ed5479be655b7d76aba558526b2dc774a3 --- /dev/null +++ b/20200116/objects-10.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; + void (* print) (t_object *this); +} t_base; + +typedef struct +{ + int type; + void (* print) (t_object *this); + int content; +} t_integer; + +typedef struct +{ + int type; + void (* print) (t_object *this); + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.print = print_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.print = print_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + + return 0; +} diff --git a/20200116/objects-11.c b/20200116/objects-11.c new file mode 100644 index 0000000000000000000000000000000000000000..a15c8eb239b2315845f24f3b4f111d3670d7ea33 --- /dev/null +++ b/20200116/objects-11.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +union t_object; + +typedef struct +{ + int type; + void (* print) (union t_object *this); +} t_base; + +typedef struct +{ + int type; + void (* print) (union t_object *this); + int content; +} t_integer; + +typedef struct +{ + int type; + void (* print) (union t_object *this); + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.print = print_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.print = print_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + + return 0; +} diff --git a/20200116/objects-12.c b/20200116/objects-12.c new file mode 100644 index 0000000000000000000000000000000000000000..5d605e1b295e252b2d947a1d4ac04c9fc805d36d --- /dev/null +++ b/20200116/objects-12.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> + +union t_object; + +typedef struct +{ + void (* print) (union t_object *this); +} t_base; + +typedef struct +{ + void (* print) (union t_object *this); + int content; +} t_integer; + +typedef struct +{ + void (* print) (union t_object *this); + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.print = print_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.print = print_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + + return 0; +} diff --git a/20200116/objects-13.c b/20200116/objects-13.c new file mode 100644 index 0000000000000000000000000000000000000000..81ef279b060e0b6290194fdeda8c3330cb716cdd --- /dev/null +++ b/20200116/objects-13.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> + +union t_object; +struct t_vmt; + +typedef struct +{ + struct t_vmt *vmt; +} t_base; + +typedef struct +{ + struct t_vmt *vmt; + int content; +} t_integer; + +typedef struct +{ + struct t_vmt *vmt; + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +typedef struct t_vmt +{ + void (* print) (union t_object *this); +} t_vmt; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_vmt vmt_integer = { print_integer }; +t_vmt vmt_string = { print_string }; + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.vmt = &vmt_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->integer.vmt = &vmt_string; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->base.vmt->print (object[i]); + + return 0; +} diff --git a/20200116/objects-2.c b/20200116/objects-2.c new file mode 100644 index 0000000000000000000000000000000000000000..a47cfb4276085399afb86795d04b1f6ae20c95bf --- /dev/null +++ b/20200116/objects-2.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + return 0; +} diff --git a/20200116/objects-3.c b/20200116/objects-3.c new file mode 100644 index 0000000000000000000000000000000000000000..ff9224c0767ccad39f5b1396720ee73b6a0455fb --- /dev/null +++ b/20200116/objects-3.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", object[i]->content); + + return 0; +} diff --git a/20200116/objects-4.c b/20200116/objects-4.c new file mode 100644 index 0000000000000000000000000000000000000000..ef7bffe80471d4b014258824421dce0557fc41dd --- /dev/null +++ b/20200116/objects-4.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", (t_integer *) object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", (t_string *) object[i]->content); + + return 0; +} diff --git a/20200116/objects-5.c b/20200116/objects-5.c new file mode 100644 index 0000000000000000000000000000000000000000..820181d87e2a04b81cd2e03aa7980d970cd6c1a6 --- /dev/null +++ b/20200116/objects-5.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", ((t_integer *) object[i])->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", ((t_string *) object[i])->content); + + return 0; +} diff --git a/20200116/objects-6.c b/20200116/objects-6.c new file mode 100644 index 0000000000000000000000000000000000000000..86847c9085083e2d5093026619550fb272b5f4ec --- /dev/null +++ b/20200116/objects-6.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == 1) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == 2) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + print_object (object[i]); + + return 0; +} diff --git a/20200116/objects-7.c b/20200116/objects-7.c new file mode 100644 index 0000000000000000000000000000000000000000..b9b24ad65f650aeda194e4570ae853e1a5ad8f11 --- /dev/null +++ b/20200116/objects-7.c @@ -0,0 +1,43 @@ +#include <stdio.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +int main (void) +{ + t_integer i = { T_INTEGER, 42 }; + t_string s = { T_STRING, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s, NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20200116/objects-8.c b/20200116/objects-8.c new file mode 100644 index 0000000000000000000000000000000000000000..0c93e8f6c3c4b5e504c758dfa87b8510e98c0692 --- /dev/null +++ b/20200116/objects-8.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +t_integer *new_integer (int i) +{ + t_integer *p = malloc (sizeof (t_integer)); + p->type = T_INTEGER; + p->content = i; + return p; +} + +t_string *new_string (char *s) +{ + t_string *p = malloc (sizeof (t_string)); + p->type = T_STRING; + p->content = s; + return p; +} + +int main (void) +{ + t_base *object[] = { (t_base *) new_integer (42), + (t_base *) new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20200116/objects-9.c b/20200116/objects-9.c new file mode 100644 index 0000000000000000000000000000000000000000..41468b043cd69b1c19b2c4456eb937ff34dbe9f8 --- /dev/null +++ b/20200116/objects-9.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_object (t_object *this) +{ + if (this->base.type == T_INTEGER) + printf ("Integer: %d\n", this->integer.content); + else if (this->base.type == T_STRING) + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20200116/pgslides.sty b/20200116/pgslides.sty new file mode 120000 index 0000000000000000000000000000000000000000..da062f7454c898ca1bc89bc29f268d9f00b21e5b --- /dev/null +++ b/20200116/pgslides.sty @@ -0,0 +1 @@ +../20200102/pgslides.sty \ No newline at end of file diff --git a/20200116/unions-1.c b/20200116/unions-1.c new file mode 100644 index 0000000000000000000000000000000000000000..b75a6063b50193e13e9a2fd0ce140db6a8827499 --- /dev/null +++ b/20200116/unions-1.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + int8_t i; + uint8_t u; +} num8_t; + +int main (void) +{ + num8_t test; + test.i = -1; + printf ("%d\n", test.u); + return 0; +} diff --git a/20200116/unions-2.c b/20200116/unions-2.c new file mode 100644 index 0000000000000000000000000000000000000000..be8562a50395e50e007dc1c0b19b98a476935670 --- /dev/null +++ b/20200116/unions-2.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + char s[8]; + uint64_t x; +} num_char_t; + +int main (void) +{ + num_char_t test = { "Hello!" }; + printf ("%lx\n", test.x); + return 0; +} diff --git a/README.md b/README.md index 8985f87d5730e652b5efa1f7fab977a6847ee7f5..b5b3929d7e4b8ce4f3f4fba38749e12dc63fbb24 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Vortragsfolien und Beispiele: * [19.12.2019: Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191219/hp-20191219.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20191219/) * [02.01.2020: Quantencomputer, Datensicherheit und Datenschutz](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20200102/hp-20200102.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20200102/) * [09.01.2020: Rekursion, Aufwandsabschätzungen, objektorientierte Programmierung](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20200109/hp-20200109.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20200109/) + * [16.01.2020: objektorientierte Programmierung, dynamische Speicherverwaltung](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20200116/hp-20200116.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20200116/) * [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/hp-slides-2019ws.pdf) Übungsaufgaben: diff --git a/hp-slides-2019ws.pdf b/hp-slides-2019ws.pdf index 01e2773d41e8a2c023cba7620493e9ee7407a021..e58c987c624249785e642c33b8475ad5a6c6db0b 100644 Binary files a/hp-slides-2019ws.pdf and b/hp-slides-2019ws.pdf differ diff --git a/hp-slides-2019ws.tex b/hp-slides-2019ws.tex index d2cf57e289b32a0c0013ab672ed4fcf593fbcc4a..c976bd232a8ca75a32615f181e26bd6bc81ac5a9 100644 --- a/hp-slides-2019ws.tex +++ b/hp-slides-2019ws.tex @@ -34,4 +34,8 @@ \includepdf[pages=-]{20191219/hp-20191219.pdf} \pdfbookmark[1]{02.01.2020: Quantencomputer, Datensicherheit und Datenschutz}{20200102} \includepdf[pages=-]{20200102/hp-20200102.pdf} + \pdfbookmark[1]{09.01.2020: Rekursion, Aufwandsabschätzungen, objektorientierte Programmierung}{20200109} + \includepdf[pages=-]{20200109/hp-20200109.pdf} + \pdfbookmark[1]{16.01.2020: objektorientierte Programmierung, dynamische Speicherverwaltung}{20200116} + \includepdf[pages=-]{20200116/hp-20200116.pdf} \end{document}