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

Vorbereitung 16.1.2020

parent 3c4adc94
Branches
No related tags found
No related merge requests found
Showing
with 1397 additions and 1 deletion
......@@ -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}
......
../common/Tower_of_Hanoi.jpeg
\ No newline at end of file
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
../common/hello-gtk.png
\ No newline at end of file
File added
This diff is collapsed.
../common/landau-symbols-2.pdf
\ No newline at end of file
../common/landau-symbols-3.pdf
\ No newline at end of file
../common/landau-symbols.pdf
\ No newline at end of file
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment