realloc kopiert nur einen teil der daten

Gast #1098596
Lesenswert?

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 #1098886
Lesenswert?

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
Lesenswert?

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
Lesenswert?

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

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren