Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
sizeof(unions + struct + bitfield)

 
Post new topic   Reply to topic Printable version
 View previous topic  Visual Basic 6.0 Anyone? Post :: Post I got bored and made a Tic-Tac-Toe gam...  View next topic  
Author Message
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Mon Dec 21, 2009 3:59 pm    Post subject: sizeof(unions + struct + bitfield) Reply to topic Reply with quote

Code: Show/Hide

typedef struct
{
   unsigned isMapObject    :1;
   unsigned objectID       :15;

   union /* Unnamed union */
   {
      struct
      {
         signed   xCoord                 :16;
         signed   yCoord                 :16;
      }mapObjectCoord;
      struct
      {
         unsigned xReference             :4;             //Coordinate reference in x, see LvzScreenObjectReference
         signed   xCoord                 :12;    //X coordinate
         unsigned yReference             :4;             //Coordinate reference in y, see LvzScreenObjectReference
         signed   yCoord                 :12;    //Y coordinate
      }screenObjectCoord; //Only used for CLV2 screenobjects
   };


   unsigned imageNumber    :8;
   unsigned objectLayer    :8;
   unsigned displayTime    :12;
   unsigned displayMode    :4;
} LvzObjectDefinition;


Ok... what am I doing wrong here?
I want sizeof(LvzObjectDefinition) to be 10 bytes... This struct up here gives me 12... I thought it could have to do with padding that is added, I removed 2 bytes in the non-union part, and it still gave me 12...

I have all the object definitions in a byte array, I just need to do like:
Code: Show/Hide
addObject((LvzObjectDefinition*)dataPtr);
dataPtr += sizeof(LvzObjectDefinition);


put that in a loop until I reach objectCount.
I thought this was a good place to use an union for the first time, but is it screwing it up?
_________________
DCME co-developer
17th Parallel Head Sysop
Subspace: The Future


Last edited by Samapico on Mon Dec 21, 2009 4:04 pm, edited 2 times in total
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Mon Dec 21, 2009 4:00 pm    Post subject: Reply to topic Reply with quote

#pragma pack(1)
_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:34
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3603
Location: Las Vegas
Offline

PostPosted: Fri Dec 25, 2009 11:50 pm    Post subject: Reply to topic Reply with quote

Bak wrote:
#pragma pack(1)

As a bit more, the reason for this is that by default, your code works faster if everything is on the byte mark (8 bits). This way, if you have a structure that has parts split on the byte mark (say 12 bits to one, 4 bits to another), to get the 12 byte part, your computer has to read the word, then mask away the parts it doesn't care about, and shift it. Same for the 4 bit part. That is a lot of work.

So instead of always trusting the programmer, it just aligns them up on the byte marks, so at worst it would have to mask away part of the word it reads in, but not shift. For Subspace, they tried to fit as much as possible in as little as possible to save bytes sent over the network when a 28k modem was considered fast, thus why you have to use this packing feature with structures.

If you look at most network games (ie: Quake), they waste tons of packet space. But they found out that they didn't need to fit as much as possible, because they didn't have to worry that 80 people would all be very close to each other, and the average bandwidth most users would have is at most 5KB/s.
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
Dr Brain
Flip-flopping like a wind surfer


Age:31
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sat Dec 26, 2009 10:36 am    Post subject: Reply to topic Reply with quote

I think you mean the 4-byte marks (on x86 at least; it's 8/16 on x86_64). #pragma pack(1) aligns things to the byte marks. I don't think either one changes how the bitfields are done.
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Sat Dec 26, 2009 11:08 am    Post subject: Reply to topic Reply with quote

Bak answered this one-liner after a more detailed answer on ssforum; I got around the problem, and I actually don't even need that struct anymore. To ensure complete portability, I filled in my non-packed struct from the raw data with bit shifts and masks
Back to top
View users profile Send private message Add User to Ignore List
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 73 page(s) served in previous 5 minutes.

phpBB Created this page in 0.073899 seconds : 30 queries executed (42.6%): GZIP compression disabled