Qbasicnews.com

Full Version: Structure of TYPEs in memory...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'd like to learn how TYPEs are built up in memory, like how each element of the type are placed accordingly to eachother etc, doesnt matter if its C/C++...anyone knows of anywhere to read about it? (yes I've tried google Tongue)...
Thanks in advance.
http://www.agner.org/assem/

Get the "Calling Conventions" document.
Types (structures in asm/c/pascal) are compiler dependant. Most modern compilers use dword alignment so that 32-bit processors can access the data they refer to faster. This means that the smallest amount of memory that any element will use is 32-bits (4 bytes, one dword). The data itself will be referenced as the type specified, a short for example, but the next element will start at the next 32-bit boundary.

FreeBasic, assemblers and C compilers will allow you to override the alignment so that you can byte-pack the data. For most cases this is not useful unless you are a) targetting a specific memory goal (ie: your structure contains a lot of bytes and would take up a huge amount of memory being dword aligned) or b) have a data format to match (either a network packet or information in a file).

Example code (FreeBasic):
Code:
'' Naturally aligned structures

Type udt_Ship_Data

   Name     As String                  ''  + 0   + 4
   Class    As String                  ''  + 4   + 4
   Cargo    As Integer                 ''  + 8   + 4
   Damage   As Integer                 ''  +12   + 4
   Shields  As Integer                 ''  +16   + 4

End Type                               ''  =20   =20

Type udt_Player_Data

   Name     As String                  ''  + 0   + 4
   Age      As String                  ''  + 4   + 4
   Cash     As Integer                 ''  + 8   + 4

   Status   As Byte                    ''  +12   + 1
   Frame    As Byte                    ''  +16   + 1

   Ship     As udt_Ship_Data Ptr       ''  +20   + 4

   Location As udt_Network_Packet Ptr  ''  +24   + 4

End Type                               ''  =28   =22


'' Byte-aligned structure

Type udt_Network_Packet Field = 1

   Player   As Integer                 ''  + 0   + 4
   X        As Short                   ''  + 4   + 2
   Y        As Short                   ''  + 6   + 2
   Heading  As Short                   ''  + 8   + 2
   Facing   As Short                   ''  +10   + 2
   Velocity As Short                   ''  +12   + 2
   Action   As Short                   ''  +14   + 2

End Type                               ''  +16   +16

For somethings, the player and ship structures, alignment is useful for speed and would not mean an excess in memory waste. Network packets, however, you want to minimize the data sent, so a little bit of speed loss for the savings in network packet size is important.

In this case, the network packets would be 28 bytes. This doesn't sound like much until you factor in sending it at least 15 times a second to as many players as are in the game. If you have a game of 32 players you would have a waste of 5760 bytes per second, or 43% wasted bandwidth.

The point in me telling you this is to illustrate where this use useful to override the field alignment. As can be seen, for networking, this is very important. I means the difference between having a 32 player game at 15 updates per second and a 16 player game at 10 updates per second. Of course, in this example you would have a centralized server which would only send relavent packets, but that is another story Wink

Anyway, hope this helps.
Thanks! 8)
You can get the size in bytes of any structure in FB like this:
len( mycustomtype )

...just a footnote to what the other dudes are saying.