Tutorium #5

Einführung in die (strukturierte) Programmierung

Gruppe 2 – Thomas Neff prog-tutor-thomas@iicm.tugraz.at
Gruppe 3 – Michael Schwarz prog-tutor-michael@iicm.tugraz.at
Gruppe 5 – Jörg Müller prog-tutor-joerg@iicm.tugraz.at
Gruppe 6 – Christoph Hack prog-tutor-hack@iicm.tugraz.at
Gruppe 8 – Anja Karl prog-tutor-anja@iicm.tugraz.at
Gruppe 9 – Manuel Weber prog-tutor-weber@iicm.tugraz.at

Tutoriumsinhalt

HW4 Besprechung

HW4 Beispiele - Gut oder schlecht?

Strings - auch nur Arrays

Strings

Vergleichen

Strings

Zugriff auf einzelne Buchstaben

Strings

Pointer (Wiederholung)

Pointer und Pointee (Wiederholung)

Verwendung von Pointern: Dynamischer Speicher

Beispiel

1int main() { 2 int* mem = (int*)malloc(sizeof(int) * 2); 3 if(mem == NULL) return 0; 4 *mem = 1; 5 *(mem + 1) = 2; 6 mem[0] = 3; 7 mem[1] = 4; 8 free(mem); return 0; }
0x1000
0x1004
0x1008
0x100C
0x1010
0x1000
0x1004
0x1008
0x100C
mem   0x10100x1000
0x1000
0x1004
0x1008
0x100C
mem   0x10100x1000
0x10001
0x1004
0x1008
0x100C
mem   0x10100x1000
0x10001
0x10042
0x1008
0x100C
mem   0x10100x1000
0x10003
0x10042
0x1008
0x100C
mem   0x10100x1000
0x10003
0x10044
0x1008
0x100C
mem   0x10100x1000
0x1000
0x1004
0x1008
0x100C
mem   0x10100x1000

Call-by-Reference

Beispiel

4void getHM(int minutes, int* h, int* m) { 5 *h = minutes / 60; 6 *m = minutes % 60; } 1int main() { 2 int hour = 0, minutes = 0; 3 getHourMinutes(320, &hour, &minutes); 7 printf("%d:%d\n", hour, minutes); return 0; }
0x1000
0x1004
0x1008
0x100C
0x2000
0x2004
0x2008
0x200C
hour   0x10000
minutes   0x10040
0x1008
0x100C
0x2000
0x2004
0x2008
0x200C
hour   0x10000
minutes   0x10040
0x1008
0x100C
min   0x2000320
h   0x20040x1000
m   0x20080x1004
0x200C
hour   0x10000
minutes   0x10040
0x1008
0x100C
min   0x2000320
h   0x20040x1000
m   0x20080x1004
0x200C
hour   0x10005
minutes   0x10040
0x1008
0x100C
min   0x2000320
h   0x20040x1000
m   0x20080x1004
0x200C
hour   0x10005
minutes   0x100420
0x1008
0x100C
min   0x2000320
h   0x20040x1000
m   0x20080x1004
0x200C
hour   0x10005
minutes   0x100420
0x1008
0x100C
0x2000
0x2004
0x2008
0x200C
stdout5:20

Valgrind

Beispiel

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char* text = (char*)malloc(6 * sizeof(char));
  strcpy(text, "Hallo!");
  printf("%s\n", text);
  return 0;
}
Kompilieren:
gcc -Wall -g -o valgrind_test valgrind_test.c
Valgrind:
valgrind --leak-check=full ./valgrind_test

Weitere Valgrind-Beispiele

Häufige Fehler / Best Practice

Warum nicht einfach?

1int* x = (int*)malloc(3); 2x = (int*)realloc(x, 16); 3if(x == NULL) { 4 free(x); 5 printf("Out of memory!"); }
x   0x10000x1008
0x1004
0x1008
0x100C
0x1010
x   0x1000NULL
0x1004
0x1008
0x100C
0x1010
x   0x1000NULL
0x1004
0x1008
0x100C
0x1010
x   0x1000NULL
0x1004
0x1008
0x100C
0x1010
x   0x1000NULL
0x1004
0x1008
0x100C
0x1010
stdoutOut of memory!

Deshalb mit Hilfspointer

1int* x = (int*)malloc(3); 2int* new_x = (int*)realloc(x, 16); 3if(new_x != NULL) { x = new_x; } 4else { 5 free(x); 6 printf("Out of memory!\n"); }
x   0x10000x1008
0x1004
0x1008
0x100C
0x1010
x   0x10000x1008
new_x   0x1004NULL
0x1008
0x100C
0x1010
x   0x10000x1008
new_x   0x1004NULL
0x1008
0x100C
0x1010
x   0x10000x1008
new_x   0x1004NULL
0x1008
0x100C
0x1010
x   0x10000x1008
new_x   0x1004NULL
0x1008
0x100C
0x1010
x   0x10000x1008
new_x   0x1004NULL
0x1008
0x100C
0x1010
stdoutOut of memory!

Gruppenassignment

Beginn der Gruppensuche:

Bitte verwendet das Tutorium oder die Newsgroup
tu-graz.lv.esp.uebungsgruppen bzw.
tu-graz.lv.ep.uebungsgruppen
um eine passende 4er-Gruppe zu finden.

Die Gruppe kann innerhalb der gesamten Tutoriumsgruppe gebildet werden.

Abgabe


Bis zum nächsten Mal!