Skip to content

munmap()

Purpose

Unmaps a memory mapping created by mmap().

#include <sys/mman.h>

Prototype

int munmap(void *addr, size_t length);

Parameters

  • addr: start address returned by mmap().
  • length: mapping length.

Return Value

  • Success: returns 0.
  • Failure: returns -1 and sets errno.

Minimal Example

if (munmap(ptr, sizeof(struct shared_data)) < 0) {
    perror("munmap");
}

Common Pitfalls

  • Use the same length that was used for mmap().
  • Do not access the pointer after munmap().
  • munmap() does not remove a POSIX shared memory object; use shm_unlink() for that.