0

In C++ there is only new and delete, but in C there is also realloc(). I think that sometimes, realloc() won't delete then reallocate memory but just extend it instead, which is faster, is this true ? If it is, then why we can't do that in the C++ style ?

  • [`realloc` is also available in C++](http://www.cplusplus.com/reference/cstdlib/realloc/) – JHBonarius Aug 28 '20 at 18:32
  • 1
    `std::vector` is probably what you want. – drescherjm Aug 28 '20 at 18:33
  • 1
    You want [std::vector](https://en.cppreference.com/w/cpp/container/vector). – Jesper Juhl Aug 28 '20 at 18:34
  • You simply write your own, creating a new block of memory of the desired new size and assign the address of the new block to a temporary pointer, copy the current data to the new block, then delete the current block and assign the address of the new block from the temporary pointer to the current pointer. – David C. Rankin Aug 28 '20 at 18:35
  • 3
    realloc() is not safe for types that aren't trivial as it won't properly destroy/construct the objects in the allocation. You can't copy the raw memory for an object of a non-trivial type somewhere else and expect things to work. This is why it is almost never used in C++ code. – cdhowie Aug 28 '20 at 18:36
  • 2
    Second of what @cdhowie states -- Never mix, `new`, `delete` and `realloc`. – David C. Rankin Aug 28 '20 at 18:37
  • @JHBonarius "In the C++ style" – linternetsansfil Aug 28 '20 at 19:36
  • @drescherjm and Jesper Juhl I am talking about raw memory allocation. – linternetsansfil Aug 28 '20 at 19:37
  • @DavidC.Rankin I said that realloc() can EXTEND a block of memory not only moving it. – linternetsansfil Aug 28 '20 at 19:38
  • @cdhowie I didn't mention that I needed to put objects in the allocated memory. – linternetsansfil Aug 28 '20 at 19:40
  • 3
    No, there is no guarantee of `realloc` extending rather than replacing a block. There are a host of variables (memory fragmentation, sufficient space beyond the current allocation, not to mention the individual compiler's implementation of `realloc()` that determine whether that is even possible). Notwithstanding the fact whether a block is extended or replaced may save a most a copy, the extension or new allocation are roughly equivalent. – David C. Rankin Aug 28 '20 at 19:44
  • 2
    @linternetsansfil Nonetheless, this is one reason why it's typically not used in C++. That and you typically allocate dynamic memory in C++ with `new`, and you can't `realloc()` an allocation that comes from `new`. You would need to use `malloc()` instead, and then `free()` to release the memory. If you were going to use `realloc()` in generic code for example, you would need to switch allocation/reallocation/free behavior based on `std::is_trivial`. This is possible, but usually not worth it for the (fairly rare) case that `realloc()` can extend without copying. – cdhowie Aug 28 '20 at 20:02
  • @DavidC.Rankin As I said "sometimes". – linternetsansfil Aug 28 '20 at 20:10
  • If you want to use "in the C++ style", don't use `realloc()`... use `std::vector`. – JHBonarius Aug 29 '20 at 09:54
  • @JHBonarius ... – linternetsansfil Aug 29 '20 at 10:40

0 Answers0