Archiv verlassen und diese Seite im Standarddesign anzeigen : c: realloc mit 2-fachem array
Hallo,
Sprache: C
ich möchte ein 2-faches array (char** speicher)
mit realloc immer genau auf die größe bringen die es braucht:
int x=0;
int y;
char **speicher;
for (..) {
x++;
speicher = realloc(speicher, x); // FEHLER
y = 1; // \0 muss noch hinzu
for (..) {
y++;
speicher[x-1] = realloc(speicher[x-1],y);
speicher[x-1][y-2] = my_byte;
}
speicher[x-1][y-1] = 0;
}
jedoch bekomme ich schon beim ersten realloc eine Fehlermeldung:
Segmentation Fault.
Wo ist mein Fehler?
Danke Markus
anda_skoa
21-12-2002, 10:48
Wie hast du "speicher" initialisiert?
Durch einen malloc Aufruf, oder hast du es auf NULL gesetzt?
Vielleicht hast du keines von beiden gemacht.
Ciao,
_
Original geschrieben von anda_skoa
Wie hast du "speicher" initialisiert?
Durch einen malloc Aufruf, oder hast du es auf NULL gesetzt?
Vielleicht hast du keines von beiden gemacht.
Ciao,
_
ich hab ihn gar nicht initialisiert,
wie muss ich das machen?
anda_skoa
21-12-2002, 11:09
Original geschrieben von msi
ich hab ihn gar nicht initialisiert,
wie muss ich das machen?
char **speicher = NULL;
Ciao,
_
da hab ihc jetzt aber verständnisprobleme,
wieso muss ich den so initalisieren?
du gibts ja praktisch dem speicher zeiger den Wert NULL.
d.h. er zeigt ins nichts.
aber durch den realloc aufruf bekommt er ja nen neuen speicherbereich,
somit wird NULL durch diesen wert wieder überschrieben
und auf das NULL wird gar nie zugefriffen.
Markus
The Ripper
21-12-2002, 13:35
Wenn du den Zeiger nicht initialisierst, zeigt er auf irgendeine Stelle im Speicher.
Diese Stelle versucht realloc() nun zu vergrössern und segfaulted, weil die Speicherstelle nicht mit malloc() reserviert wurde (und das Programm somit kein Recht hat, da irgendwas rumzuschrauben)
Wenn du nun den Zeiger mit NULL initialisierst, verhält sich realloc() genau wie malloc().
ah danke
jetzt hab ichs kapiert.
ich komm jetzt wieder nicht weiter:
mein programm:
#include <stdio.h>
#include <stdlib.h>
int main() {
int x=0;
int y;
int a,b;
char** speicher = NULL;
for (a=0;a<10;a++) {
x++;
printf("| For %i speicher[%i]\n",a,x);
speicher = realloc(speicher, x);
if (!speicher) {
exit(1);
}
printf(" --- realloc survived\n");
y = 1; // \0 muss noch hinzu
for (b=0;b<10;b++) {
printf(" | For %i: ",b);
y++;
printf(" - Relloc: speicher[%i][%i]: ", x-1,y);
fflush(stdout);
speicher[x-1] = realloc(speicher[x-1],y);
printf(" ok\n");
fflush(stdout);
if (! speicher[x-1]) exit(1);
printf(" - byte in speicher[%i][%i]: ",x-1,y-2);
fflush(stdout);
speicher[x-1][y-2] = 'a';
printf("%c\n", speicher[x-1][y-2]);
}
printf(" | Zero Byte in speicher[%i][%i]\n", x-1,y-1);
speicher[x-1][y-1] = 0;
}
printf("OK\n\n");
printf("%c\n", speicher[1][1]);
}
der output:
.
.
.
- byte in speicher[1][3]: a
| For 4: - Relloc: speicher[1][6]: ok
- byte in speicher[1][4]: a
| For 5: - Relloc: speicher[1][7]: ok
- byte in speicher[1][5]: a
| For 6: - Relloc: speicher[1][8]: ok
- byte in speicher[1][6]: a
| For 7: - Relloc: speicher[1][9]: ok
- byte in speicher[1][7]: a
| For 8: - Relloc: speicher[1][10]: ok
- byte in speicher[1][8]: a
| For 9: - Relloc: speicher[1][11]: ok
- byte in speicher[1][9]: a
| Zero Byte in speicher[1][10]
| For 2 speicher[3]
--- realloc survived
| For 0: - Relloc: speicher[2][2]: ok
- byte in speicher[2][0]: a
| For 1: - Relloc: speicher[2][3]: ok
- byte in speicher[2][1]: a
| For 2: - Relloc: speicher[2][4]: ok
- byte in speicher[2][2]: a
| For 3: - Relloc: speicher[2][5]: ok
- byte in speicher[2][3]: a
| For 4: - Relloc: speicher[2][6]: ok
- byte in speicher[2][4]: a
| For 5: - Relloc: speicher[2][7]: ok
- byte in speicher[2][5]: a
| For 6: - Relloc: speicher[2][8]: ok
- byte in speicher[2][6]: a
| For 7: - Relloc: speicher[2][9]: ok
- byte in speicher[2][7]: a
| For 8: - Relloc: speicher[2][10]: ok
- byte in speicher[2][8]: a
| For 9: - Relloc: speicher[2][11]: ok
- byte in speicher[2][9]: a
| Zero Byte in speicher[2][10]
| For 3 speicher[4]
--- realloc survived
| For 0: - Relloc: speicher[3][2]: Segmentation fault
[root]@[sun]:[~/source/test]
was ist da nun wieder falsch?
danke Markus
Hi,
das Prob. ist das gleiche wie vorher. Bei speicher[x-1] = realloc(...) ist speicher[x-1] ja auch noch nicht initialisiert.
ahhh jetzt gehts!!
vielen dank an euch alle.
man jetzt komm ich schon wieder nicht weiter,
ich will eine ganze datei per realloc in ein
2 faches char array speicher:
.
.
.
char **scryptoin;
int cryptoin, cryptoinl;
cryptoinl=1;
cryptoin=0;
scryptoin=NULL;
scryptoin = realloc(scryptoin,1);
scryptoin[0]=NULL;
while ( (i=fgetc(fcryptoin)) != EOF) {
if (i=='\n') {
scryptoin[cryptoinl-1] = realloc(scryptoin[cryptoinl-1],cryptoin+1);
scryptoin[cryptoinl-1][cryptoin]=0;
printf("line %i: %s\n", cryptoinl-1, scryptoin[cryptoinl-1]);
printf("realloc for line %i\n\n", cryptoinl+1);
scryptoin=realloc(scryptoin,++cryptoinl);
scryptoin[cryptoinl-1] = NULL;
cryptoin=0;
} else {
scryptoin[cryptoinl-1] = realloc(scryptoin[cryptoinl-1], cryptoin+1);
cryptoin++;
scryptoin[cryptoinl-1][cryptoin-1]=i;
printf(" [%i][%i]: %c\n", cryptoinl-1,cryptoin-1,scryptoin[cryptoinl-1][cryptoin-1]);
a_chiffre[i]++;
}
}
printf(" == line %i:%i : add \\0 ==\n", cryptoinl-1,cryptoin);
scryptoin[cryptoinl-1] = realloc(scryptoin[cryptoinl-1],cryptoin+1);
scryptoin[cryptoinl-1][cryptoin]=0;
printf("line %i: %s\n", cryptoinl-1, scryptoin[cryptoinl-1]);
fclose(fcryptoin);
printf("Reading finished\n\n");
for (i=0;i<cryptoinl;i++) {
printf("Line %i: %s\n", i, scryptoin[i] );
}
bei kleinen dateien geht das ja gut,
aber sobald es eine größere ist gibts Probleme
und wieder ein Segmentation fault.
ich hab ewig nach diesem Fehler gesucht und nichts
gefunden, vielleicht könnt ihr mir helfen..
Markus
hallo kann mir den keiner helfen??
The Ripper
25-12-2002, 19:43
Vielleicht geht dir bei grösseren Dateien der Speicher aus?!? Oder er findet keinen zusammenhängenen Speicherblock der gewünschten Grösse?
Prüfe doch nach realloc()-Aufrufen, ob ein Fehler aufgetreten ist
Powered by vBulletin® Version 4.2.5 Copyright ©2025 Adduco Digital e.K. und vBulletin Solutions, Inc. Alle Rechte vorbehalten.