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,487 of 33,346   
   Juan Pedro Bolivar Puente to All   
   Re: deleting an array of objects and und   
   24 Sep 11 14:42:36   
   
   From: raskolnikov@gnu.org   
      
   >   
   > Indeed, think about this code:   
   >   
   >     X* p = new Y[2];   
   >     x[1].foo = 1;   
   >   
   > That is UB too, because x[1] which is *(x+1) relies on its knowledge of   
   > sizeof(X) to know how much to advance the memory address 'p' -- i.e. if   
   > sizeof(X) < sizeof(Y), it would most probably overwrite data of the   
   > first element instead of what you intended to do. Thus, it is impossible   
   > to treat arrays polymorphically, because you always need the size of the   
   > concrete type of its element to access the array. Just think of 'delete   
   > []' of one of those other operations that rely on the concrete type,   
   > because of how 'delete []' might be implemented.   
   >   
      
   To avoid further misunderstandings, I mean 'x' when I say 'p' and the   
   first line on the code above should read 'X* x = ...'.   
      
   Sorry for the confusion.   
      
   JP   
      
      
   --   
         [ 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