• Advertisement

Archived

This topic is now archived and is closed to further replies.

{word dword} 8 bytes?

This topic is 6061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

    
#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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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--

Share this post


Link to post
Share on other sites
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--

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Its the same way on the PS2, except its 128 bits aligned because it has a 128bit proccessor. However, there are a lot of instructions that are 64bit, so it is possible (and faster) to feed it two of the 64bit instructions at the same time if you micro code it correctly. It runs faster and is really neat.

Things will always be faster if you pass the proccessor what I wants most. Which is why most bools are ints, because it is a lot faster. You could write a class to use just 1 bit, but then I''d reccommend making it a whole int and just representing each bit as its own bool. Oh well I''m just rambling now. :p

Share this post


Link to post
Share on other sites
So as a general tip, it''s good to tell the compiler to align for 32 bits? Or do you get errors with non- Intel cpu''s then?

I think Lamoth says something about this in Tricks of the Game Programming Gurus, but I''m not sure



Humanity''s first sin was faith; the first virtue was doubt

Share this post


Link to post
Share on other sites
As a general tip, it''s good to let the compiler do what it thinks is best. Most compilers will align to "natural alignment" by default. This is whatever alignment is best for the particular structure.

Unless you have a specific reason to change it, just leave the default as-is. For the vast majority of people the only reason to change it is because you need to match the memory layout of some structure that you didn''t define.

-Mike

Share this post


Link to post
Share on other sites

  • Advertisement