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,474 of 33,346    |
|    bartek szurgot to All    |
|    Re: Is it ever unsafe to realloc?    |
|    12 Jul 12 12:23:44    |
      From: basz@no.spam              On 07/12/2012 03:40 AM, 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?              short answer is: it will NOT work.              in general case it will not work for non-POD types. for example       consider class Foo having std::string as a member. using realloc on a       block of memory, containing instance of Foo and internal string's       data, would be the same as if you'd memcpy instance of Foo yourself.              notice that this approach breaks encapsulation, since from now on you       need to keep track of ALL internal implementations and types in used,       straight to the bottom, in order to be sure such a "memcpy" situation       does not take place.              --       pozdrawiam serdecznie / best regards,       bartek szurgot       /* http://www.baszerr.eu */                      [ 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