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,475 of 33,346    |
|    Francis Glassborow to All    |
|    Re: Is it ever unsafe to realloc?    |
|    12 Jul 12 12:31:12    |
      From: francis.glassborow@btinternet.com              On 12/07/2012 02: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?              Definitely not. If you use realloc all the data that the program is       aware of will get moved and in doing so your objects will cease to be       accessible. That means that they will not be destructable.              There really is no point in trying to provide a memory pool that way.       Just provide a large enough pool to start with (and probably do that       the C++ way by using a call to operator new to get raw memory)              If necessary you can always create a second pool.              Francis                     --        [ 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