Add module1_memory.c
This commit is contained in:
113
src/c_examples/module1_memory.c
Normal file
113
src/c_examples/module1_memory.c
Normal file
@@ -0,0 +1,113 @@
|
||||
// ============================================
|
||||
// Modul 1: Memory-Modell
|
||||
// Beispiele in C
|
||||
// ============================================
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Beispiel 1.1: Stack vs Heap
|
||||
void demo_memory_regions() {
|
||||
// STACK: Automatisch verwaltet
|
||||
int stack_value = 42; // Lebt nur in dieser Funktion
|
||||
int stack_array[100]; // Automatisch freigegeben beim Return
|
||||
|
||||
// HEAP: Manuell verwaltet
|
||||
int* heap_value = malloc(sizeof(int));
|
||||
*heap_value = 42;
|
||||
|
||||
int* heap_array = malloc(100 * sizeof(int));
|
||||
|
||||
// WICHTIG: Heap-Speicher muss freigegeben werden!
|
||||
free(heap_value);
|
||||
free(heap_array);
|
||||
}
|
||||
|
||||
// Beispiel 1.2: Memory Leak erkennen
|
||||
void bad_example() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int* leak = malloc(1024); // ⚠️ Allokiert, nie freigegeben!
|
||||
// free(leak); // Fehlt!
|
||||
}
|
||||
}
|
||||
|
||||
void good_example() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int* no_leak = malloc(1024);
|
||||
// ... Nutzung ...
|
||||
free(no_leak); // ✅ Korrekt freigegeben
|
||||
}
|
||||
}
|
||||
|
||||
// Beispiel 1.3: Use-after-free
|
||||
void use_after_free_demo() {
|
||||
int* ptr = malloc(sizeof(int));
|
||||
*ptr = 100;
|
||||
free(ptr);
|
||||
|
||||
// ⚠️ DANGER: ptr zeigt jetzt auf ungültigen Speicher!
|
||||
// *ptr = 200; // UNDEFINED BEHAVIOR!
|
||||
|
||||
// ✅ Korrekt: Pointer auf NULL setzen nach free
|
||||
ptr = NULL;
|
||||
}
|
||||
|
||||
// Beispiel 1.4: Stack Frame Visualisierung
|
||||
void inner_function(int x) {
|
||||
int local = x * 2; // Auf Stack von inner_function
|
||||
printf("inner_function: x=%d, local=%d\n", x, local);
|
||||
// 'local' wird hier automatisch freigegeben
|
||||
}
|
||||
|
||||
void outer_function() {
|
||||
int outer_var = 10; // Auf Stack von outer_function
|
||||
inner_function(outer_var);
|
||||
// 'outer_var' existiert noch hier
|
||||
printf("outer_function: outer_var=%d\n", outer_var);
|
||||
}
|
||||
|
||||
// Beispiel 1.5: Dynamische 2D-Array Allokation
|
||||
int** create_matrix(int rows, int cols) {
|
||||
// Array von Pointern (Heap)
|
||||
int** matrix = malloc(rows * sizeof(int*));
|
||||
|
||||
for (int i = 0; i < rows; i++) {
|
||||
matrix[i] = malloc(cols * sizeof(int)); // Jede Zeile separat
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void free_matrix(int** matrix, int rows) {
|
||||
for (int i = 0; i < rows; i++) {
|
||||
free(matrix[i]); // Jede Zeile freigeben
|
||||
}
|
||||
free(matrix); // Pointer-Array freigeben
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// Hauptprogramm
|
||||
// ============================================
|
||||
int main() {
|
||||
printf("=== Modul 1: Memory-Modell ===\n\n");
|
||||
|
||||
printf("--- Stack/Heap Demo ---\n");
|
||||
demo_memory_regions();
|
||||
|
||||
printf("\n--- Stack Frame Demo ---\n");
|
||||
outer_function();
|
||||
|
||||
printf("\n--- Matrix Allokation ---\n");
|
||||
int** mat = create_matrix(3, 3);
|
||||
mat[1][1] = 42;
|
||||
printf("matrix[1][1] = %d\n", mat[1][1]);
|
||||
free_matrix(mat, 3);
|
||||
|
||||
printf("\n=== Konzept verstanden? ===\n");
|
||||
printf("1. Stack: Automatisch, schnell, begrenzt\n");
|
||||
printf("2. Heap: Manuell, flexibel, langsam\n");
|
||||
printf("3. malloc() muss mit free() gepaart sein\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user