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,608 of 33,346   
   Kim Walisch to All   
   Re: Strict aliasing and marshalling   
   25 Oct 12 11:10:55   
   
   From: kim.walisch@googlemail.com   
      
   { Please limit your text to fit within 80 columns, preferably around 70,   
     so that readers don't have to scroll horizontally to read each line.   
     This article has been reformatted manually by the moderator. -mod }   
      
   > unsigned char* buffer = new char[...];   
   > v32 = *((uint32_t*)(buffer)) //unaligned reads??   
      
   The C++ standard requires that memory returned by new is at least   
   aligned to sizeof(void*). As far as I know GCC aligns memory to   
   sizeof(void*) * 2 (= 8 for 32-bit, = 16 for 64-bit), so you won't   
   get an unaligned read here. But there is an other problem, v32 will   
   have different values on little-endian and big-endian CPUs:   
      
   little-edian v32 = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) |   
   (buffer[3] << 24);   
   big-edian    v32 = buffer[3] | (buffer[2] << 8) | (buffer[1] << 16) |   
   (buffer[0] << 24);   
      
   Your cast C-style cast (uint32_t*) is in fact a C++ reinterpret_cast   
   which should be avoided most of the time. Your memcpy code is not   
   endian safe either.   
      
      
   --   
         [ 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