{word dword} 8 bytes?

Started by
11 comments, last by Clash Rocker 22 years, 10 months ago
    
#include <iostream.h>

typedef unsigned long  DWORD;
typedef unsigned short WORD;

typedef struct
{
    WORD m_w;
    DWORD m_d;
}AAA;

typedef struct
{
    DWORD m_d1;
    DWORD m_d2;
}BBB;

typedef struct
{
    WORD m_w1;
    WORD m_w2;
}CCC;

int main(void)
{
    cout<<"sizeof(WORD): "<<sizeof(WORD)<<endl;
    cout<<"sizeof(DWORD): "<<sizeof(DWORD)<<endl;
    cout<<"sizeof(AAA): "<<sizeof(AAA)<<" WORD + DWORD"<<endl;
    cout<<"sizeof(BBB): "<<sizeof(BBB)<<" DWORD + DWORD"<<endl;
    cout<<"sizeof(CCC): "<<sizeof(CCC)<<" WORD + WORD"<<endl;

    return 0;
}    
OK, why does sizeof(AAA) report 8 bytes? That was under g++ (2.x) and MSVC 6.0 omf.com Edited by - Clash Rocker on June 18, 2001 1:05:59 AM
Advertisement
The compiler adds an extra 4bytes to align the data structure.
Memory transfers are considerably faster if just falls on 64/128/256/512bit boundries; I don''t know the exact technical reason why, it has to do with cache page sizes, bus bit widths, and the memory gating/multiplexing address lines.

You can turn it off in the project settings, and with a compiler directive, pack something, I never use it...

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
In MSVC: Go to project settings, into "C/C++" tab and choose "code generaion". Then check "struct member aligment". It''s 8 bytes for default. You can choose another aligment (2 bytes, for example), but i think the program will run faster with 8 bytes struct aligment
You could also swap the two entries and have the DWORD first, and as it aligns to a dword it will not need to add in packing bytes to the structure (not in between the values anyway)



Dæmin
(Dominik Grabiec)
sdgrab@eisa.net.au
Daemin(Dominik Grabiec)
Ahhh
OK, I was completely f****ed up for awhile there.
Thanks fellas

3D PC ARCADE FIGHTER
I saw something like this... The way it worked was if it was on a 1/2/4/8/16/32/64(on the pent 4 only) byte boundary, it could move the whole thing around with one instruction. However, if by chance it ended up as 7/31/63 byte boundary, you get stuck with like 8 instructions. In fact, if it''s on a 32 byte boundary (or 64 byte on the pent 4) it''ll go even faster. No idea why, though, but it really makes its mark when writing to the agp bus like in DDraw. Saw this in some x86 journal, I think, or maybe it was game developer mag...
The reason is because Intel processors (since the days of the 386) had a 32-bit wide bus. That is, when transferring data from memory into cache/registers/other memory, it does it 32 bits at a time. Even if you only want 8-bits transferred, you still had to transfer the full 32 bits and then mask the data you want. Luckily, the masking is not difficult, but it''s a bit slower than accessing the full 32-bits. Also, accessing the lower 8- or 16- bits of a number is quite fast (since the 32-bit eax register is also accessed with the 16-bit ax registor or the 8-bit ah and al registers) at least compared to accessing the upper 16 bits of a number (there''s no axh or whatever register)

War Worlds - A 3D Real-Time Strategy game in development.
This issue is even worse on RISC processors. Accessing unaligned data isn''t just slow - it crashes. The OS can catch the crash and fix things up for you but that amounts to making a call to the OS every time you want to read (unaligned) data.

-Mike
-Mike
related question:
is there a way to declare a single structure as "packed"?

I ran into this alignment problem once when writing a lib that loaded BMPs... a book I have gives you the structure to read in the header info, which starts with the bytes "bm". The book has that header in the structure, but when I was trying to use it, my program was reading in four bytes instead of two (apparently because of this alignment issue)... and thus corrupting the rest of the struct''s data... so is there a way to declare the struct that would fix this?

fyi, the way I fixed this was to read in the header seprately, then the header struct

--Tr][aD--
--Tr][aD--
oh, for the curious it seems this works...
#pragma pack(1) // 1-byte structure packing.struct MyStruct {// whatever members.}#pragma pack() // return to default packing. 


found on another post... this is why it''s always a good idea to do a search before posting a question... chances are it''s been asked already

--Tr][aD--
--Tr][aD--

This topic is closed to new replies.

Advertisement