C++ – Features of the mremap function in Linux

Features of the mremap function in Linux… here is a solution to the problem.

Features of the mremap function in Linux

In Linux, the mremap function is used to remap memory mapped using mmap. Please help me clarify the following:

  1. If the mremap function fails, what is the state of the old mapped memory?
  2. If the mremap function fails, do I need to call the munmap function?
  3. If the mremap function succeeds, is there previous data in remapped memory?


MreMap attempts to increase the allocation in place, but falls back to allocating a new region if it cannot increase the size of the current region.

mremap() expands (or shrinks) an existing memory mapping, potentially moving it at the same time (controlled by the flags argument and the available virtual address space). src

  1. If the mremap fails, the old memory is fine (like realloc).

  2. If mremap fails, there is nothing to munmap (at least from this call). See item 1.

  3. If the mremap succeeds and must be moved, the old memory is copied to the new memory (the old memory does the munmap for you). If the mremap is able to increase the size in place, memory is not moved and no new allocations are created.

Related Problems and Solutions