I know that trivially copyable objects can safely be copied my malloc
into an appropriate storage location1 and that the destination object will have the same value as the source.
Is this also possible with realloc
? That is, if realloc
some storage containing some objects of type T
, and realloc
decides to move and copy the block, will the objects in the newly allocated storage be intact and have started their lifetime, and will the lifetime of the objects in the old storage be safely ended?
1 While asking this question, I had assumed that an "appropriate storage location" included uninitialized storage of suitable alignment and size, but as M.M's answer below argues this isn't actually well supported by the standard. That would make realloc
questionable since it is always copying into uninitialized storage.