Jump to content

  • Log In with Google      Sign In   
  • Create Account

{word dword} 8 bytes?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Clash Rocker   Members   -  Reputation: 130

Like
Likes
Like

Posted 17 June 2001 - 06:04 PM

    
#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

Sponsor:

#2 Shannon Barber   Moderators   -  Reputation: 1362

Like
Likes
Like

Posted 17 June 2001 - 06:11 PM

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


#3 Tolik   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 June 2001 - 06:20 PM

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

#4 Dæmin   Members   -  Reputation: 128

Like
Likes
Like

Posted 17 June 2001 - 06:22 PM

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

#5 Clash Rocker   Members   -  Reputation: 130

Like
Likes
Like

Posted 17 June 2001 - 06:24 PM

Ahhh
OK, I was completely f****ed up for awhile there.
Thanks fellas

3D PC ARCADE FIGHTER

#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 17 June 2001 - 09:35 PM

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...

#7 Dean Harding   Members   -  Reputation: 546

Like
Likes
Like

Posted 17 June 2001 - 11:22 PM

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.

#8 Anon Mike   Members   -  Reputation: 1098

Like
Likes
Like

Posted 18 June 2001 - 06:37 AM

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


#9 TrIaD   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 June 2001 - 07:00 AM

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

#10 TrIaD   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 June 2001 - 07:20 AM

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

#11 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 18 June 2001 - 07:21 AM

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

#12 SkyRat   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 June 2001 - 07:48 AM

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

#13 Anon Mike   Members   -  Reputation: 1098

Like
Likes
Like

Posted 19 June 2001 - 07:14 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS