home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,609 of 33,346   
   Martin Bonner to All   
   Re: Strict aliasing and marshalling   
   25 Oct 12 11:19:44   
   
   From: martinfrompi@yahoo.co.uk   
      
   On Wednesday, October 24, 2012 7:44:29 PM UTC+1, fmatthew5876 wrote:   
   > With strict aliasing enabled, what is the most correct way   
   > to marshal/unmarshal binarydata?   
   >   
   > unsigned char* buffer   
   > uint32_t v32   
   >   
   >   
   > Would you use one of these?   
   > memcpy(&v32, buffer, sizeof(v32))   
   >   
   > v32 = *((uint32_t*)(buffer)) //unaligned reads??   
      
   No.  (Although the memcpy is marginally safer than the   
   unaligned read).   
      
   > Would you do something else?   
   I'd use:   
     v32 = ((uint32_t)buffer[2] << 24) |   
           ((uint32_t)buffer[3] << 16) |   
           ((uint32_t)buffer[0] <<  8) |   
           ((uint32_t)buffer[1]          ;   
   Obviously the actual order of the bytes in the buffer will depend   
   on the marshalling format. (0,1,2,3 or 3,2,1,0 will be the commonest   
   choices).   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca