Forum: PC-Programmierung realloc kopiert nur einen teil der daten


von Michael H. (Gast)


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

von Sven P. (Gast)


Lesenswert?

Quelltext...?

von Michael H. (Gast)


Angehängte Dateien:

Lesenswert?

betreffende datei im anhang...

von Michael H. (Gast)


Angehängte Dateien:

Lesenswert?

falsche datei... hier kommt die richtige

von Rufus Τ. F. (rufus) Benutzerseite


Lesenswert?

Die Headerdatei brauchts auch noch, ohne die Typdefinitionen kann man da 
nur raten.

von Peter (Gast)


Lesenswert?

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.

von Michael H. (Gast)


Angehängte Dateien:

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]$

von Rufus Τ. F. (rufus) Benutzerseite


Lesenswert?

Was jetzt noch fehlt, sind die merkwürdigen Funktionen, deren Name in 
Versalien (GROSSBUCHSTABEN) geschrieben werden, wie z.B. UDS_COLUM_SIZE.

von Michael H. (Gast)


Angehängte Dateien:

Lesenswert?

Anbei ist das Makro...

von Michael H. (Gast)


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]$

von Michael H. (Gast)


Lesenswert?

need help... plz help me...

von P. S. (Gast)


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.

von Michael H. (Gast)


Lesenswert?

und wie komme ich dann auf den richtigen Speicherblock?

von Peter (Gast)


Lesenswert?

uds_cur_colum++;

von P. S. (Gast)


Lesenswert?

Oder einfach mal nach Pointerarithmetik suchen und das grundsaetzlich 
nachlesen. Noch besser waere ein C-Buch.

von Michael H. (Gast)


Lesenswert?

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
Noch kein Account? Hier anmelden.