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,425 of 33,346   
   Francis Glassborow to Seungbeom Kim   
   Re: Casting from void*   
   27 Aug 11 05:56:41   
   
   From: francis.glassborow@btinternet.com   
      
   On 27/08/2011 03:19, Seungbeom Kim wrote:   
   > On 2011-06-28 12:12, Seungbeom Kim wrote:   
   >> On 2011-06-27 11:02, A. McKenney wrote:   
   >>>   
   >>>     By far the most common usage of   
   >>>     reinterpret_cast<>  in the code I   
   >>>     work on is between (char *) and   
   >>>     (unsigned char *) (or their const   
   >>>     equivalents.)   
   >>>   
   >>>     I consider this "practically portable",   
   >>>     because I am not aware of any   
   >>>     implementation where it would make   
   >>>     sense for the pointer representation to   
   >>>     differ between the signed and unsigned   
   >>>     versions.   
   >>>   
   >>>     Can anyone conceive of an architecture where   
   >>>     a standard-conforming C++ implementation would   
   >>>     have any reason to use different representations   
   >>>     for  signed char * and unsigned char *?   
   >>   
   >> I can't, especially when the standard guarantees that the three   
   >> character types have the same object representation, including   
   >> the same alignment requirements [3.9.1/1], and even that char*   
   >> have the same representation and alignment requirements as void*   
   >> [3.9.2/4].   
   >>   
   >> Why should char* be more closely related to void* than to signed char*   
   >> or unsigned char*? Why doesn't the standard simply guarantee that the   
   >> three character pointer types also have the same object representation,   
   >> and that they can be converted by static_cast among one another?   
   >>   
   >> Assuming the status quo, is it correct that we have to go through void*   
   >> to convert among the three character types, e.g.   
   >>   
   >>     signed char* scp = /* ... */;   
   >>     unsigned char* ucp =   
   >>         static_cast(static_cast(scp));   
   >>   
   >> or   
   >>   
   >>     signed char* scp = /* ... */;   
   >>     void* vp = scp;   
   >>     unsigned char* ucp = static_cast(vp);   
   >>   
   >> to be strictly conforming?   
   >>   
   >> In addition, what about conversion from any object pointer to a   
   >> character pointer: I guess we should go through void* here as well.   
   >> Am I correct?   
   >   
   > I'm sad to find out that none of these questions have been answered.   
   > Can anyone shed some light on them, please? :-)   
      
   Yes, it is an error to try to cast between pointers of unrelated types   
   (and the fundamental types are all unrelated, related types are ones   
   where one is a pointer to a type that is derived from the type the other   
   is pointing to^). If you really feel the need to take a char * and write   
   it to an unsigned char * you could use a reinterpret_cast   
      
   ^ No that pointers to pointers are always unrelated types and cannot be   
   assigned to each other unless the type being pointed to is the same in   
   both cases:   
      
   class Base {};   
   class Derived: public Base {};   
      
   int main(){   
       Derived d;   
       Derived * d_ptr(&d);   
       Base * b_ptr(d_ptr);  // OK   
       Derived ** d_ptr2ptr (&d_ptr);   
       Base ** b_ptr2ptr(d_ptr2ptr); // error   
   }   
      
      
   --   
         [ 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