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,473 of 33,346   
   Seungbeom Kim to All   
   Re: Is it ever unsafe to realloc?   
   12 Jul 12 12:19:36   
   
   From: musiphil@bawi.org   
      
   On 2012-07-11 18:40, fmatthew5876 wrote:   
   > Suppose I have a pool of memory (say a dynamic array) that I   
   > allocate using malloc(), cleanup using free(), and use placement new   
   > and explicit destructor calls to handle constructors/destructors.   
   >   
   > If I want to resize this memory pool (invalidating any iterators/   
   > pointers to objects in the pool) can I safely call realloc() to do it   
   > no matter what kind of C++ objects are being stored? Are there some   
   > classes of C++ types where I have to allocate a new pool using malloc()   
   > and use move/copy constructors? If so what are the cases where realloc   
   > is not safe?   
      
   realloc() potentially allocates a new block and bit-copies (as with   
   memcpy()) the old contents into the new block. This is safe for POD   
   objects, but not in general: the copy (or move) constructor should   
   handle the relocation.   
      
   --   
   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