Skip to content
Snippets Groups Projects
Commit c0ae7b1c authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Vorbereitung 28.1.2021

parent a24dd28c
No related branches found
No related tags found
No related merge requests found
Showing
with 2904 additions and 0 deletions
#include <stdio.h>
#include <stdint.h>
typedef struct
{
uint32_t a;
uint64_t b;
uint8_t c;
} three_numbers;
int main (void)
{
three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
printf ("%s\n", &xyz);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strcpy (sls.first_name, "Sabine");
strcpy (sls.family_name, "Leutheusser-Schnarrenberger");
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int wings;
} with_wings;
typedef struct with_legs
{
int legs;
} with_legs;
int main (void)
{
animal *a[2];
animal duck;
a[0] = &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
a[0]->wings = 2;
animal cow;
a[1] = &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
a[1]->legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int wings;
} with_wings;
typedef struct with_legs
{
int legs;
} with_legs;
int main (void)
{
animal *a[2];
animal duck;
a[0] = &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
((with_wings *) a[0])->wings = 2;
animal cow;
a[1] = &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
((with_legs *) a[1])->legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
../common/hello-gtk.png
\ No newline at end of file
File added
This diff is collapsed.
File added
This diff is collapsed.
File added
% hp-uebung-20210128.pdf - Exercises on Low-Level Programming
% Copyright (C) 2013, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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: Speicherformate von Zahlen, Personen-Datenbank, objektorientierte Tier-Datenbank
\documentclass[a4paper]{article}
\usepackage{pgscript}
\begin{document}
% \thispagestyle{empty}
\section*{Hardwarenahe Programmierung\\
Übungsaufgaben -- 28.\ Januar 2021}
Diese Übung enthält Punkteangaben wie in einer Klausur.
Um zu "`bestehen"', müssen Sie innerhalb von 100 Minuten
unter Verwendung ausschließlich zugelassener Hilfsmittel
17 Punkte (von insgesamt \totalpoints) erreichen.
\exercise{Speicherformate von Zahlen}
Wir betrachten das folgende Programm (\gitfile{hp}{2020ws/20210128}{aufgabe-1.c}):
\begin{lstlisting}
#include <stdio.h>
#include <stdint.h>
typedef struct
{
uint32_t a;
uint64_t b;
uint8_t c;
} three_numbers;
int main (void)
{
three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
printf ("%s\n", &xyz);
return 0;
}
\end{lstlisting}
Das Programm wird für einen 32-Bit-Rechner compiliert und ausgeführt.\\
(Die \lstinline[style=cmd]{gcc}-Option \lstinline[style=cmd]{-m32} sorgt dafür,
daß \lstinline[style=cmd]{gcc} Code für einen 32-Bit-Prozessor erzeugt.)
\begin{lstlisting}[style=terminal]
$ ¡gcc -Wall -m32 aufgabe-2.c -o aufgabe-2¿
aufgabe-2.c: In function "main":
aufgabe-2.c:14:13: warning: format "%s" expects argument of type "char *", but
argument 2 has type "three_numbers * {aka struct <anonymous> *}" [-Wformat=]
printf ("%s\n", &xyz);
^
$ ¡./aufgabe-2¿
Hallo, Welt!
\end{lstlisting}
\begin{enumerate}[\quad(a)]
\item
Erklären Sie die beim Compilieren auftretende Warnung.
\points{2}
\item
Erklären Sie die Ausgabe des Programms.
\points{4}
\item
Welche Endianness hat der verwendete Rechner?
Wie sähe die Ausgabe auf einem Rechner mit entgegengesetzter Endianness aus?
\points{2}
\item
Dasselbe Programm wird nun für einen 64-Bit-Rechner compiliert und ausgeführt.\\
(Die \lstinline[style=cmd]{gcc}-Option \lstinline[style=cmd]{-m64} sorgt dafür,
daß \lstinline[style=cmd]{gcc} Code für einen 64-Bit-Prozessor erzeugt.)
\begin{lstlisting}[style=terminal,gobble=8]
$ ¡gcc -Wall -m64 aufgabe-2.c -o aufgabe-2¿
aufgabe-2.c: In function "main":
aufgabe-2.c:14:13: warning: format "%s" expects argument of type "char *",
but argument 2 has type "three_numbers * {aka struct <anonymous> *}"
[-Wformat=]
printf ("%s\n", &xyz);
^
$ ¡./aufgabe-2¿
Hall5V
\end{lstlisting}
(Es ist möglich, daß die konkrete Ausgabe auf Ihrem Rechner anders aussieht.)\par
Erklären Sie die geänderte Ausgabe des Programms.
\points{3}
\end{enumerate}
\clearpage
\exercise{Personen-Datenbank}
Wir betrachten das folgende Programm (\gitfile{hp}{2020ws/20210128}{aufgabe-2.c}):
\begin{lstlisting}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strcpy (sls.first_name, "Sabine");
strcpy (sls.family_name, "Leutheusser-Schnarrenberger");
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
\end{lstlisting}
Die Standard-Funktion \lstinline{strcpy()} bewirkt ein Kopieren eines Strings
von rechts nach links, hier also z.\,B.\ die Zuweisung der String-Konstanten
\lstinline{"Sabine"} an die String-Variable \lstinline{sls.first_name[]}.
Das Programm wird für einen 32-Bit-Rechner compiliert und ausgeführt.\\
(Die \lstinline[style=cmd]{gcc}-Option \lstinline[style=cmd]{-m32} sorgt dafür,
daß \lstinline[style=cmd]{gcc} Code für einen 32-Bit-Prozessor erzeugt.)
\begin{lstlisting}[style=terminal]
$ ¡gcc -Wall -O -m32 aufgabe-2.c -o aufgabe-2¿
$ ¡./aufgabe-2¿
Sabine Leutheusser-Schnarrenberger wurde am 110.98.1701278309 geboren.
Speicherzugriffsfehler
\end{lstlisting}
\begin{enumerate}[\quad(a)]
\item
Erklären Sie die Ausgabe des Programms einschließlich der Zahlenwerte.
\points{4}
\item
Welche Endianness hat der verwendete Rechner?
Begründen Sie Ihre Antwort.
\points{1}
\item
Wie sähe die Ausgabe auf einem Rechner mit entgegengesetzter Endianness aus?
\points{2}
\item
Erklären Sie den Speicherzugriffsfehler.
(Es kann sein, daß sich der Fehler auf Ihrem Rechner nicht bemerkbar macht.
Er ist aber trotzdem vorhanden.)
\points{2}
\end{enumerate}
\clearpage
\exercise{Objektorientierte Tier-Datenbank}
Das unten dargestellte Programm (Datei: \gitfile{hp}{2020ws/20210128}{aufgabe-3a.c})
soll Daten von Tieren verwalten.
Beim Compilieren erscheinen die folgende Fehlermeldungen:
\begin{lstlisting}[style=terminal]
$ ¡gcc -std=c99 -Wall -O aufgabe-2a.c -o aufgabe-2a¿
aufgabe-2a.c: In function 'main':
aufgabe-2a.c:31: error: 'animal' has no member named 'wings'
aufgabe-2a.c:37: error: 'animal' has no member named 'legs'
\end{lstlisting}
Der Programmierer nimmt die auf der nächsten Seite in Rot dargestellten Ersetzungen vor\\
(Datei: \gitfile{hp}{2020ws/20210128}{aufgabe-3b.c}).
Daraufhin gelingt das Compilieren, und die Ausgabe des Programms lautet:
\begin{lstlisting}[style=terminal]
$ ¡gcc -std=c99 -Wall -O aufgabe-2b.c -o aufgabe-2b¿
$ ¡./aufgabe-2b¿
A duck has 2 legs.
Error in animal: cow
\end{lstlisting}
\begin{itemize}
\item[(a)]
Erklären Sie die o.\,a.\ Compiler-Fehlermeldungen.
\points{2}
\item[(b)]
Wieso verschwinden die Fehlermeldungen nach den o.\,a.\ Ersetzungen?
\points{3}
\item[(c)]
Erklären Sie die Ausgabe des Programms.
\points{5}
\item[(d)]
Beschreiben Sie -- in Worten und/oder als C-Quelltext -- einen Weg,
das Programm so zu berichtigen, daß es die Eingabedaten
(``A duck has 2 wings. A cow has 4 legs.'') korrekt speichert und ausgibt.\\
\points{4}
% \item[(e)]
% Schreiben Sie das Programm so um,
% daß es keine expliziten Typumwandlungen mehr benötigt.\par
% Hinweis: Verwenden Sie \lstinline{union}.
% \points{4}
% \item[(f)]
% Schreiben Sie das Programm weiter um,
% so daß es die Objektinstanzen \lstinline{duck} und \lstinline{cow}
% dynamisch erzeugt.\par
% Hinweis: Verwenden Sie \lstinline{malloc()} und schreiben Sie Konstruktoren.
% \points{4}
% \item[(g)]
% Schreiben Sie das Programm weiter um,
% so daß die Ausgabe nicht mehr direkt im Hauptprogramm erfolgt,
% sondern stattdessen eine virtuelle Methode \lstinline{print()}
% aufgerufen wird.\par
% Hinweis: Verwenden Sie in den Objekten Zeiger auf Funktionen,
% und initialisieren Sie diese in den Konstruktoren.
% \points{4}
\end{itemize}
\begin{minipage}[t]{0.34\textwidth}
\begin{lstlisting}[gobble=6,xleftmargin=0pt]
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int wings;
} with_wings;
typedef struct with_legs
{
int legs;
} with_legs;
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{0.65\textwidth}
\begin{lstlisting}[gobble=6,xleftmargin=0pt]
int main (void)
{
animal *a[2];
animal duck;
a[0] = &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
a[0]->wings = 2;
animal cow;
a[1] = &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
a[1]->legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
\end{lstlisting}
\begin{picture}(0,0)
\color{red}
\put(3.7,6.207){\vector(-1,0){0.7}}
\put(3.8,6.207){\makebox(0,0)[l]{\lstinline[basicstyle=\color{red}]{((with_legs *) a[1])->legs = 4;}}}
\put(4.0,8.735){\vector(-1,0){0.7}}
\put(4.1,8.735){\makebox(0,0)[l]{\lstinline[basicstyle=\color{red}]{((with_wings *) a[0])->wings = 2;}}}
\end{picture}
\end{minipage}
\begin{flushright}
\textit{Viel Erfolg!}
\end{flushright}
\makeatletter
\immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}}
\makeatother
\end{document}
#include <stdio.h>
#include <stdint.h>
typedef struct
{
uint32_t a;
uint64_t b;
uint8_t c;
} three_numbers;
int main (void)
{
three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
printf ("%x %x %x\n", xyz.a, xyz.b, xyz.c);
printf ("%08x %016x %02x\n", xyz.a, xyz.b, xyz.c);
printf ("%s\n", &xyz);
return 0;
}
#include <stdio.h>
#include <stdint.h>
typedef struct
{
uint32_t a;
uint64_t b;
uint8_t c;
} three_numbers;
int main (void)
{
three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
printf ("%x %lx %x\n", xyz.a, xyz.b, xyz.c);
printf ("%08x %016lx %02x\n", xyz.a, xyz.b, xyz.c);
printf ("%s\n", &xyz);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
strcpy (sls.first_name, "Sabine");
strcpy (sls.family_name, "Leutheusser-Schnarrenberger");
sls.day = 26;
sls.month = 7;
sls.year = 1951;
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strncpy (sls.first_name, "Sabine", 10);
strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 20);
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strncpy (sls.first_name, "Sabine", 9);
strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 19);
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strncpy (sls.first_name, "Sabine", 9);
sls.first_name[9] = 0;
strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 19);
sls.family_name[19] = 0;
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
animal duck;
a[0] = &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
((with_wings *) a[0])->wings = 2;
animal cow;
a[1] = &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
((with_legs *) a[1])->legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
with_wings duck;
a[0] = (animal *) &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
((with_wings *) a[0])->wings = 2;
with_legs cow;
a[1] = (animal *) &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
((with_legs *) a[1])->legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
with_wings duck;
a[0] = (animal *) &duck;
duck.type = WITH_WINGS;
duck.name = "duck";
duck.wings = 2;
with_legs cow;
a[1] = (animal *) &cow;
cow.type = WITH_LEGS;
cow.name = "cow";
cow.legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment