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 31,379 of 33,346   
   Seungbeom Kim to Joshua Maurice   
   Re: Casting from void*   
   01 Jul 11 07:58:54   
   
   88e53233   
   From: musiphil@bawi.org   
      
   On 2011-06-30 23:10, Joshua Maurice wrote:   
   > On Jun 28, 12:10 pm, Seungbeom Kim  wrote:   
   >>   
   >> You did make a claim that when going to or from void*, static_cast and   
   >> reinterpret_cast are equivalent; i.e. static_cast(A*) is equivalent   
   >> to reinterpret_cast(A*), and static_cast(void*) is equivalent   
   >> to reinterpret_cast(void*). This is not guaranteed to hold if the   
   >> representations of A* and void* are different.   
   >   
   > True. Sorry. I've been lost in the ambiguity of some of the English.   
   > Are you aware of an actual implementation where this is not the case?   
   > Just curious.   
      
   No, I'm not.   
      
   > Again, I'm not saying you should rely on this. I'm   
   > merely noting that all implementations of which I'm aware do it this   
   > way, and if you need to debug bad code, it's good to know that this is   
   > how it happens to work out in practice - though not necessarily by   
   > standard.   
      
   It may be worthwhile to know what works on "most" implementations,   
   or even to know what works on just those I use, but I was talking   
   about what the standard guarantees.   
      
   >>   
   >> I'm not yet convinced. If void* is represented by byte indices,   
   >> A* by 2-byte indices and B* by 4-byte indices, the former expression   
   >> will correctly convert the 4-byte index to a 2-byte index (by * 4 / 2),   
   >> but the latter will not. Did I miss anything in this example?   
   >   
   > Work with me. In both cases, you're going from a pointer that is   
   > stored as a 4-byte value, and going to a pointer that is stored as a 2-   
   > byte value. Simple application of the pigeonhole principle shows that   
   > you're going to get collisions no matter how you map it. It's broken   
   > either way. I'm not exactly sure of what semantics you're proposing,   
   > so you would have to be more clear please.   
      
   My terminology might have been confusing. (Sorry, I tried to be concise   
   too much.) By a "n-byte index," I meant an index that is into an array   
   whose elements are n bytes. So, for example, if you have a 256-byte   
   memory, a byte index would be in [0, 256], a 2-byte index in [0, 128],   
   and a 4-byte index in [0, 64]. If an object starts at byte index 32,   
   a 4-byte index is represented by the value 8. And there is no collision   
   as long as the alignment requirement for each type is satisfied.   
      
   --   
   Seungbeom Kim   
      
      
         [ 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