Joshua Rutschmann 8 anni fa
parent
commit
c6fa0d2b62

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

@@ -0,0 +1,9 @@
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 Vedi File

@@ -0,0 +1,11 @@
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 Vedi File

@@ -0,0 +1,7 @@
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 Vedi File

@@ -0,0 +1,7 @@
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 Vedi File

@@ -0,0 +1,8 @@
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 Vedi File

@@ -0,0 +1,5 @@
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 Vedi File

@@ -0,0 +1,12 @@
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 Vedi File

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

Loading…
Annulla
Salva