Hallo, ich habe ein bizarres Problem mit der realloc. Es scheint nur einen Teil der daten zu kopieren, bessergesagt immer nur den 1. Datensatz. Den datensatz habe ich so erzeugt: malloc(sizeof(mytype)*anzahl). Jetzt möchte ich das ganze mit realloc vergrößern: not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a name1 1 b Wenn ich realloc mache: changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) (müsste der inhalt der 2. zeile stehen) (null) 0 (null) (hier ist es okay, dass überall null steht) Hat jemand einen Tipp für mich? Michael
Die Headerdatei brauchts auch noch, ohne die Typdefinitionen kann man da nur raten.
lass doch mal die größe anzeigen. (uds_table->colums*UDS_COLUM_SIZE()); Ich glaube kaum das realloc was falsch macht, kann also bloss eine Fehler in der größenberechnung sein.
Datenstruktur ist angehängt. Anbei die Datenstruktur: michael[uds]$ ./uds This is a mainfunction for individual testing. Later it gonna be removed! not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT changed (Reallocsize: 28: ==Analizing contents of table testing== NAME TYPE DEFAULT (null) 0 (null) not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a changed (Reallocsize: 56: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a name1 1 b changed (Reallocsize: 84: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) (null) 0 (null) ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) name2 1 c not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) name2 1 c changed (Reallocsize: 112: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) (null) 0 (null) (null) 0 (null) ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) (null) 0 (null) name3 1 d michael[uds]$
Was jetzt noch fehlt, sind die merkwürdigen Funktionen, deren Name in Versalien (GROSSBUCHSTABEN) geschrieben werden, wie z.B. UDS_COLUM_SIZE.
Nachtrag: Eigentlich müsste das Makro so heißen: #define UDS_COLUM_SIZE() (sizeof(uds_colums_t)) aber das ändert auch nichts an der Funktion. Der Output ist aber anders: michael[uds]$ ./uds This is a mainfunction for individual testing. Later it gonna be removed! not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT changed (Reallocsize: 12: ==Analizing contents of table testing== NAME TYPE DEFAULT (null) 0 (null) not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a changed (Reallocsize: 24: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a name1 1 b changed (Reallocsize: 36: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) (null) 0 (null) ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) name2 1 c not yet changed: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) name2 1 c changed (Reallocsize: 48: ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) (null) 0 (null) (null) 0 (null) ==Analizing contents of table testing== NAME TYPE DEFAULT id 1 a (null) 0 (null) (null) 0 (null) name3 1 d michael[uds]$
uds_cur_colum+=UDS_COLUM_SIZE(); Das ist gut gemeint, aber falsch. Bei Pointerarithmetik wird automatisch ein Vielfaches der Strukturgroesse addiert. Wenn du die Groesse nochmal mit rein nimmst, schreibst und liest du also irgendwo im Speicher rum, weit hinter deinem Speicherblock.
und wie komme ich dann auf den richtigen Speicherblock?
Oder einfach mal nach Pointerarithmetik suchen und das grundsaetzlich nachlesen. Noch besser waere ein C-Buch.
Oh mein Gott, war der Fehler doof! Ich dachte, ich muss zusätzlich noch die size miteinrechnen... Vielen Dank es funzt!
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.