Pārlūkot izejas kodu

Added a working null.c, malloc.c and intArray2.c. intArray1.c needs to

be reworked.
Lorenz Bung 8 gadus atpakaļ
vecāks
revīzija
d40c9b54ea
5 mainītis faili ar 41 papildinājumiem un 0 dzēšanām
  1. 9
    0
      hw2/simu1/ANSWERS.md
  2. 7
    0
      hw2/simu1/free.c
  3. 8
    0
      hw2/simu1/intArray2.c
  4. 5
    0
      hw2/simu1/malloc.c
  5. 12
    0
      hw2/simu1/null.c

+ 9
- 0
hw2/simu1/ANSWERS.md Parādīt failu

@@ -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
+

+ 7
- 0
hw2/simu1/free.c Parādīt failu

@@ -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
+}

+ 8
- 0
hw2/simu1/intArray2.c Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

@@ -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
+}

Notiek ielāde…
Atcelt
Saglabāt