Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » sizeof liefert unterschiedliche Werte

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
26.03.2008, 13:47 Uhr
RedEagle



Folgende Funktion

C++:
{
memory *current;
current = freenodes/*Global*/ = firstbyte/*Global*/ + sizeof(memory);
for(int i=2; i<MEM_MAXNODESPERPAGE; i++)
{
  current->next = current + sizeof(memory); //0xA9
  current = current->next;
}
current->next = NULL;

return sizeof(memory); //0x0D
}


C++:
struct __attribute__ ((__packed__)) memory
{
DWORD  address;
DWORD  size;
memory *next;
bool   free;
};


Wenn ich das kompiliere, bekomme ich für sizeof(memory) 2 Werte. 0x0D (Was richtig währe) und 0xA9.:

objdump:
   0:    55                       push   %ebp
   1:    89 e5                    mov    %esp,%ebp
   3:    53                       push   %ebx
   4:    8b 1d 00 00 00 00        mov    0x0,%ebx
   a:    8d 8b a9 00 00 00        lea    0xa9(%ebx),%ecx
  10:    89 0d 00 00 00 00        mov    %ecx,0x0
  16:    89 ca                    mov    %ecx,%edx
! 18:    81 c2 a9 00 00 00        add    $0xa9,%edx
  1e:    8d 83 4a cf 00 00        lea    0xcf4a(%ebx),%eax
  24:    39 c2                    cmp    %eax,%edx
  26:    89 92 5f ff ff ff        mov    %edx,0xffffff5f(%edx)
  2c:    75 ea                    jne    18 <_ZN6memory17InitFreenodesListEv+0x18>
  2e:    c7 81 a9 ce 00 00 00     movl   $0x0,0xcea9(%ecx)
  35:    00 00 00
! 38:    b8 0d 00 00 00           mov    $0xd,%eax
  3d:    5b                       pop    %ebx
  3e:    5d                       pop    %ebp
  3f:    c3                       ret    


Compiler: g++ (GCC) 4.1.2

Woran liegt das? Und wie kann ich das verhindern?
--
MFG RedEagle

Dieser Post wurde am 26.03.2008 um 13:54 Uhr von RedEagle editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
26.03.2008, 14:36 Uhr
RedEagle



Scheint doch nicht an sizeof zu liegen...
Bei

Code:
current->next = current + /*sizeof(memory)*/0x0D;

Wird folgendes berechnet:
0x00C010A9 + 0x0000000D = 0x00C01152

0x00C010A9 entspricht current
und current->next ist nach dieser Zeile 0x00C01152

Und so sollte es sein:
current = 0x00C0100D
current->next = 0x00C0101A

ps.: Damit das ganze adressen-zeugs nicht verwirrt:
Ich habe eine 4KB-Page an der Adresse 0x00C01000 (virtuell). In dieser Page soll die Liste angelegt werden.
--
MFG RedEagle

Dieser Post wurde am 26.03.2008 um 14:45 Uhr von RedEagle editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
26.03.2008, 15:49 Uhr
Tommix



Hallo,
Um einen Zeiger auf die nächste Speicherstelle zeigen zu lassen, mußt Du ihn um Eins erhöhen, das mal sizeof(...) ist implizit (0xA9 = 0x0D*0x0D).

Gruß, Tommix
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
26.03.2008, 16:21 Uhr
RedEagle



Oh mann...
Danke!
--
MFG RedEagle

Dieser Post wurde am 26.03.2008 um 16:21 Uhr von RedEagle editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: