Joshua Rutschmann 8 years ago
parent
commit
c6fa0d2b62
8 changed files with 62 additions and 0 deletions
  1. 9
    0
      hw2/simu1/ANSWERS.md
  2. 11
    0
      hw2/simu1/Makefile
  3. 7
    0
      hw2/simu1/free.c
  4. 7
    0
      hw2/simu1/intArray1.c
  5. 8
    0
      hw2/simu1/intArray2.c
  6. 5
    0
      hw2/simu1/malloc.c
  7. 12
    0
      hw2/simu1/null.c
  8. 3
    0
      hw2/task1/src/lib.rs

+ 9
- 0
hw2/simu1/ANSWERS.md View File

1
+## Antworten zur Simulation 1 (hw2)
2
+
3
+1. Beim Ausführen stürzt das Programm mit der Fehlermeldung `Segmentation Fault` ab.
4
+2. GDB gibt die Fehlermeldung `Inferior 1 (process 28881) exited with code 0213` aus.
5
+3. Valgrind zeigt den Fehler `Invalid Read`. Das bedeutet, dass auf eine undefinierte Adresse zugegriffen wird.
6
+4. Das Programm `malloc` erzeugt keine Ausgabe und crasht nicht. GDB bestätigt das: `Inferior 1 (process 2982) exited normally`. Mithilfe von Valgrind sehen wir, dass 10 Bytes nicht freigegeben wurden: `LEAK SUMMARY: definitely lost: 10 bytes in 1 blocks`.
7
+5.
8
+6. Das Programm (`intArray2`) gibt den Wert `0` aus und läuft ohne Fehler. Valgrind weist uns auf einen `invalid read` an der betreffenden Stelle hin.
9
+

+ 11
- 0
hw2/simu1/Makefile View File

1
+CC=gcc
2
+CFLAGS=-g
3
+RM=rm -f
4
+TARGET=null malloc intArray1 intArray2 free
5
+.PHONY: all clean
6
+all: $(TARGET)
7
+clean:
8
+	$(RM) null malloc intArray1 intArray2 free
9
+
10
+$(TARGET): $(TARGET).c
11
+	$(CC) $< $(CFLAGS) -o $@

+ 7
- 0
hw2/simu1/free.c View File

1
+/* free.c */
2
+#include <stdlib.h>
3
+int main(char* argv[], int argc) {
4
+  char* testString = malloc(10 * sizeof(char));
5
+  free(testString[10]);
6
+  free(testString);
7
+}

+ 7
- 0
hw2/simu1/intArray1.c View File

1
+/* intArray1.c */
2
+#include <stdlib.h>
3
+int main(char* argv[], int argc) {
4
+  int* intArray = malloc(100 * sizeof(int));
5
+  intArray[100] = 0;
6
+  free(intArray);
7
+}

+ 8
- 0
hw2/simu1/intArray2.c View File

1
+/* intArray2.c */
2
+#include <stdlib.h>
3
+#include <stdio.h>
4
+int main(char* argv[], int argc) {
5
+  int* intArray = malloc(100 * sizeof(int));
6
+  free(intArray);
7
+  printf("%d", intArray[10]);
8
+}

+ 5
- 0
hw2/simu1/malloc.c View File

1
+/* malloc.c */
2
+#include <stdlib.h>
3
+int main(char* argv[], int argc) {
4
+  char* testString = malloc(sizeof(char) * 10);
5
+}

+ 12
- 0
hw2/simu1/null.c View File

1
+#include <stdlib.h>
2
+#include <stdio.h>
3
+int main(char* argv[], int argc)
4
+{
5
+  int i, j;
6
+  int* iPointer;
7
+
8
+  i = 10;
9
+  iPointer = &i;
10
+  iPointer = NULL;
11
+  j = *iPointer;
12
+}

+ 3
- 0
hw2/task1/src/lib.rs View File

6
     }
6
     }
7
 }
7
 }
8
 pub fn hamming_distance(s1: &str, s2: &str) -> Result<usize, String> {
8
 pub fn hamming_distance(s1: &str, s2: &str) -> Result<usize, String> {
9
+    //Check if the given Strings are of different length
9
     if s1.len() != s2.len() {
10
     if s1.len() != s2.len() {
10
         return Err("Strings must be of equal length!".to_string());
11
         return Err("Strings must be of equal length!".to_string());
11
     }
12
     }
12
     let mut dist: usize = 0;
13
     let mut dist: usize = 0;
13
     for i in 0..s1.len() {
14
     for i in 0..s1.len() {
15
+        //Compare each character of the Strings
14
         if s1.chars().nth(i) != s2.chars().nth(i) {
16
         if s1.chars().nth(i) != s2.chars().nth(i) {
17
+            //If they don't match, increment hamming distance
15
             dist += 1
18
             dist += 1
16
         }
19
         }
17
     }
20
     }

Loading…
Cancel
Save