Hallo zusammen,
ich habe gerade ein Brett vor dem Kopf und hoffe ihr könnt mir
vielleicht weiterhelfen. Eigentlich will ich "nur" eine 2D Array
dynamisch anlegen. Zur einfachen Verdeutlichung meines Problems, habe
ich hier ein Minimal-Beispiel aus meinem Code erzeugt (Abfrage ob
realloc/malloc NULL zurück liefert wurde hier zur besseren Lesbarkeit
entfernt):
1 | #include <stdio.h>
|
2 | #include <stdlib.h>
|
3 | #include <string.h>
|
4 |
|
5 | void test()
|
6 | {
|
7 | int count = 10;
|
8 | int** test = NULL;
|
9 |
|
10 | for(int i=0; i<count; i++)
|
11 | {
|
12 | printf("%i\n", i);
|
13 |
|
14 | test = (int**) realloc(test, i+1 * sizeof(int*));
|
15 | test[i] = (int*) malloc(5 * sizeof(int));
|
16 | }
|
17 | }
|
Ich erhalte folgende Konsolen-Ausgabe:
1 | 0
|
2 | 1
|
3 | 2
|
4 | 3
|
5 | 4
|
6 | realloc(): invalid next size
|
7 | Aborted (core dumped)
|
Vielen Dank für eure Hilfe