2 dimensionales Array mit Funktion reallokieren.
-
Ich will mit einer Funktion ein 2-dim (rows=10 * cols=5)-Array in ein (rows=10 * cols=10)-Array reallokieren. Geht das? Wenn ja, wie? Bzw. wo liegt mein Denkfehler im untenstehenden Code?
// ------------------------------------------------------------------ void reallocArray(int **array, int rows, int cols) { int size = 0; int i; for (i = 0; i < rows ; i++) { realloc(array[i], rows * sizeof(int)); } } // ------------------------------------------------------------------ int main() { int **array = NULL; int rows, cols; int i; rows = 10; cols = 5; array = malloc(rows * sizeof(int *)); for (i = 0; i<rows ; i++) { array[i] = malloc(cols * sizeof(int)); } reallocArray(array, rows, cols); // [Free Array] return 0; }
-
Hallo!
Dein Code sieht ganz gut aus. Habe es nicht ausprobiert, vielleicht geht es mit folgender Änderung:
// ------------------------------------------------------------------ void reallocArray(int **array, int rows, int cols) { int i; for (i = 0; i < rows ; i++) { realloc(array[i], cols * sizeof(int)); /* cols statt rows */ } } // ------------------------------------------------------------------ int main() { int **array = NULL; int rows, cols; int i; rows = 10; cols = 5; array = malloc(rows * sizeof(int *)); for (i = 0; i<rows ; i++) { array[i] = malloc(cols * sizeof(int)); } cols = 10; /* cols auf 10 setzen */ reallocArray(array, rows, cols); // [Free Array] return 0; }
Gruß
Michael
-
OK, habe meinen Fehler gefunden: x = realloc(x, size) macht mehr Sinn als nur realloc(x, size).
Jetzt ist aber ein neues Problem aufgetreten:
Laut dem Buch 'Softwareentwicklung in C' von Klaus Schmaranz besteht die quadratische Vergrößerung eines 2 dim Arrays aus 3 Schritten:
+++++
+++++
+++++1. rows vektor vergrößern
array = realloc(array, new_rows * sizeof(int *);+++++
+++++
+++++
*
*
2. cols vekoren, die bereits vorhanden waren, vergrößern
for (i = 0; i < old_rows; i++)
{
array[i] = realloc(array[i], new_cols * sizeof(int));
}
+++++**
+++++**
+++++**
*
*3. neue cols vektoren erstellen
for (i = old_rows; i < new_rows; i++)
{
array[i] = malloc(new_cols * sizeof(int));
}
+++++**
+++++**
+++++**
*******
*******(new_cols == new_rows, nachdem es sich dann um ein quadratisches Array handeln soll)
Wenn ich jetzt aber ein 10*5 Array quadratisieren (10*10) will, kann ich mir Schritt 2 ja ersparen, bleiben noch Schritte 1 & 3. Wenn ich das aber versuche, kommt es beim Auslesen zu einem Fehler. Lasse ich aber Schritt 1 ebenfalls weg, so funktioniert es - WARUM?? Das darf doch nicht sein, oder? Genau das Umgekehrte hätte ich erwartet, nämlich eine Zugriffsverletzung wenn ich VERGESSE, den rows Vektor zu vergrößern.
Ein 5*10 Array (unter Verwendung des Schrittes 2 mit entsprechender) zu quadratisieren funktioniert aber wie erwartet.
Benutze übrigens lccwin32.
Hier mein code snippet:// ------------------------------------------------------------------ void transpMatrix(int **array, int rows, int cols) { int size = 0; int i, j; if (rows != cols) { if (rows > cols) { size = rows; for (i = 0; i < size ; i++) { array[i] = realloc(array[i], size * sizeof(int)); for (j = cols; j < size ; j++) { array[i][j] = 0; } } } else { size = cols; array = realloc(array, size * sizeof(int*)); // ohne diese Zeile funktioniert es for (i = rows; i < size ; i++) { array[i] = malloc(size * sizeof(int)); for (j = 0; j < size ; j++) { array[i][j] = 0; } } } } }