Thread: Can't I "munmap" a part of a given memory block?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    129

    Can't I "munmap" a part of a given memory block?

    I don't know if this is the right place to ask this (I suppose some people can consider this lower level and not strictly related to C or C++). I have allocated a memory block directly with "mmap" and while I try to unmap ("munmap" system call) a part of it, it returns the -1 code! So I don't even know what's the problem. Any ideas? And any ways to bypass this?

    Example program:

    Code:
    #include "stdio.h"
    #include "sys/mman.h"
    
    
     #define null         (void*)0
    #define DEF_PROTECT  PROT_READ | PROT_WRITE
    #define DEF_FLAGS    MAP_PRIVATE | MAP_ANON
    
    
     void first_ten(void* ptr) {
      printf("%d\n", munmap(ptr, 10));
    }
    
    
     void last_ten(void* ptr) {
      printf("%d\n", munmap(ptr + 10, 10));
    }
    
    
     int main() {
      void* mem = mmap(null, 20, DEF_PROTECT, DEF_FLAGS, 0, 0);
    
       /* Unmapping the first 10 bytes work */
      first_ten(mem);
       /* Unmapping the last 10 bytes DOES NOT work */
      last_ten(mem);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,439
    Why don't you also print errno to find out the reason for the error?

    On success, munmap() returns 0. On failure, it returns -1, and errno is set to indicate the cause of the error (probably to EINVAL).

    ERRORS
    EACCES A file descriptor refers to a non-regular file. Or a file mapping was requested, but fd is not open for reading. Or MAP_SHARED was requested and PROT_WRITE is set, but fd is not open in
    read/write (O_RDWR) mode. Or PROT_WRITE is set, but the file is append-only.

    EAGAIN The file has been locked, or too much memory has been locked (see setrlimit(2)).

    EBADF fd is not a valid file descriptor (and MAP_ANONYMOUS was not set).

    EEXIST MAP_FIXED_NOREPLACE was specified in flags, and the range covered by addr and length clashes with an existing mapping.

    EINVAL We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary).
    10 bytes into a block is not going to be page aligned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    129
    Quote Originally Posted by Salem View Post
    Why don't you also print errno to find out the reason for the error?


    10 bytes into a block is not going to be page aligned.
    I'm learning how the OS manages memory so I can hopefully understand. The code for errno is "22" which is invalid argument so you are right. This will mess up my library in the end, lol!
    Once again, thank you for your help and have a great day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 03-14-2022, 04:50 AM
  2. Can't get "return()" to work.Problem in "if" part
    By coder1 in forum C Programming
    Replies: 13
    Last Post: 09-10-2013, 01:08 AM
  3. Replies: 2
    Last Post: 11-03-2012, 08:50 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread