In place of realloc with gcc / linux

Is there such a thing? I mean some function that redistributes memory without moving it if possible, or do nothing if it is not possible. There is _expand in Visual C that does what I want. Does anyone know about equivalents for other platforms, in particular gcc / linux? What interests me most is the reduction of memory in place whenever possible (and the standard realloc can move memory even when its size is reduced, if someone asks).

I know there is no standard way to do this, and I'm directly asking about dirty hacking tricks that are implementation dependent. List everything you know that works somewhere.

+3
source share
2 answers

Apart from using mmapand munmapto eliminate excess that you do not need (or mremapthat can do the same, but non-standard), there is no way to reduce the size of the allocated memory block. And it mmaphas page granularity (usually 4k), so if you are not dealing with very large objects, using this will be worse than just leaving the objects larger and not compressing them at all.

With that said, reducing memory in place is probably not a good idea, since freed memory will be highly fragmented. A good implementation reallocwill want to move the blocks while significantly reducing them as a possibility of defragmenting the memory.

, , , , , , . , :

  • , "" , .
  • , , (size_t ptrdiff_t) .

, , .

, , - , , .

+2

. , , mmap ( MAP_ANONYMOUS) mremap MREMAP_MAYMOVE. , .

+1

Source: https://habr.com/ru/post/1716146/


All Articles