realloc kopiert nur einen teil der daten

Gast #1098596
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
Gast #1098648
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.
Gast #1098886
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]$
Angehängte Dateien:
Gast #1098906
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]$
Gast #1099773
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.

Antwort schreiben

Eine eigene Frage? Dieser Thread ist über 17 Jahre alt. Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.

Neuen Thread starten

Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.