6

I've noticed std::aligned_alloc() coming into C++17, and I like it. But - what happens when I need to reallocate? I can do this manually (assuming the available space at the currently-allocated address is just the amount of space I asked for), but shouldn't there be a facility for this in the standard library?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    After a bit of searching, I could not find anything in the standard library for explicit reallocation, but c++17 does have an `std::align()` function that would make writing your own `realloc()` not too difficult (not that it would be too difficult anyway). The link for that is: https://en.cppreference.com/w/cpp/memory/align – Alerra Aug 16 '18 at 12:27
  • Related, if not essentially a duplicate of: https://stackoverflow.com/questions/20314602/does-realloc-of-memory-allocated-by-c11-aligned-alloc-keep-the-alignment – Geezer Aug 16 '18 at 12:34
  • @Alerra: The problem is, that as a user, I don't know how much more space is available, so I have to be over-conservative; while the system libraries, or the kernel, do know; so I would allocate more often than I should... – einpoklum Aug 16 '18 at 12:34
  • That's a good point. I was searching through SO and happened on your other question regarding this (it is a very valid question), but yeah it definitely is odd that c++ doesn't have and standard library `realloc()`'s that support alignment that I know about. Maybe in a future version? – Alerra Aug 16 '18 at 12:38
  • I'm not sure if they will give you a `std::aligned_realloc()` because if you can't expand the current buffer you need to get a new one, copy everything over, and then free the old buffer. This isn't trivial if you have non trivial types. Essentially it would be rebuilding `vector`. – NathanOliver Aug 16 '18 at 12:47
  • @NathanOliver: These functions work on `void*`'s... – einpoklum Aug 16 '18 at 14:18
  • @einpoklum: Define "work". If any object in the storage to be realloced has a pointer to any object within that same storage, then the object will be broken. And that is not based on the *type* of the object, but on its current *value*. – Nicol Bolas Aug 16 '18 at 14:49
  • If you want to use realloc, so you are looking for efficiency. If you are looking for efficiency => go directly to system calls and create your own allocator. Take a look at glibc malloc implementation. This uses assumptions about hardware that are 20 years outdated. I just can bet that the same is true on MSVC. – Oliv Aug 16 '18 at 17:08

1 Answers1

5

There isn't such standard call equivalent.

Even more so, Microsoft's latest implementation of C++ still has its own _aligned_malloc() instead of the now standardized std::aligned_alloc(), and here they explain why:

aligned_alloc() will probably never be implemented, as C11 specified it in a way that’s incompatible with our implementation (namely, that free() must be able to handle highly aligned allocations).

Among their (Microsoft's) underscore-prefixed implementations they do serve you with _aligned_realloc() :-)

Geezer
  • 5,600
  • 18
  • 31