Tutorium #7

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

Gruppensuche




HW5 - Gut oder Schlecht?

int errorMemory()
{
  printf("Error: Out of memory!\n");
  return ERROR_MEM;
}

[...]

int main()
{
  char *input = (char*)malloc(STRING_LENGTH * sizeof(char));
  if(input == NULL)
  {
    errorMemory();
  }
  
  while((character_from_stdin = getchar()) != EOF)
  {
    input[input_counter] = character_from_stdin;
  
  [...]
  }
  
  [...]
  
}

HW5 - Gut oder Schlecht?

  [...]
int max_char = 32;
int pos = 0;

char *ptr = (char*) malloc(max_char * (sizeof(char)));
  
if (ptr == NULL)
{
  printf ("Error: Out of memory!\n");
  return 1; 
}

char input;
do
{
  input = getchar();
  if (pos == max_char)
  {
    max_char += 16;
    char* tmp = (char*) realloc (ptr, max_char * (sizeof(char)));
    if (tmp == NULL)
    {
      printf ("Error: Out of memory!\n");
      free(ptr);
      return 1; 
    }
    ptr = tmp;
  }
  [...]
}
[...]
  

HW5 - Gut oder Schlecht?

[...]

char* text = malloc(10000 * sizeof(char));

gets(text);

int length = strlen(text);
int i = 0;
for(i = length - 1; i >= 0; i--)
  printf("%c", text[i]);

[...]

HW5 - Gut oder Schlecht?

[...]
#define BLOCKSIZE 50

char* text = malloc(BLOCKSIZE * sizeof(char));

[...] // NULL-Pointercheck

char letter = getchar();

int index = 0;
int size = BLOCKSIZE;

while(letter != EOF)
{
  if(index == size)
  {
    size += BLOCKSIZE;
    char* tmp = realloc(text, size * sizeof(char));
    [...] //NULL-Pointer check
  }
  text[index] = letter;
  letter = getchar();
}

while(index >= 0)
{
  printf("%c", text[index]);
  index--;
}
printf("\n");
[...]

HW5 - Häuftigste Fehler

Strukturen

Strukturen

Strukturen dynamisch anlegen

Selbstreferenzierende Strukturen

File I/O

struct in Datei speichern

__attribute__((packed)) ?

struct _Code_ {
0:char symbol;
1:uint8_t bits;
2:0 (Padding)
3:0 (Padding)
4:char* code;
}
struct _Code_ {
0:char symbol;
1:uint8_t bits;
2:char* code;
}__attribute__((packed))

struct aus Datei lesen

Wie schaut ein int in einer Datei aus?

Beispiel zur Umrechnung

Beispieldatei von HW 6 - Hex-Editor


Geht das auch einfacher?

HW6 - Dateigröße ermitteln

Abgabe

Bis zum nächsten Mal!