Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1048973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:32:31+00:00 2026-05-16T16:32:31+00:00

I have some code (which I cannot change) that I need to get working

  • 0

I have some code (which I cannot change) that I need to get working in a native Win32 environment. This code calls mmap() and munmap(), so I have created those functions using CreateFileMapping(), MapViewOfFile(), etc., to accomplish the same thing. Initially this works fine, and the code is able to access files as expected. Unfortunately the code goes on to munmap() selected parts of the file that it no longer needs.

x = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
... 
munmap(x, hdr_size);
munmap(x + foo, bar);
...

Unfortunately, when you pass a pointer into the middle of the mapped range to UnmapViewOfFile() it destroys the entire mapping. Even worse, I can’t see how I would be able to detect that this is a partial un-map request and just ignore it.

I have tried calling VirtualFree() on the range but, unsurprisingly, this produces ERROR_INVALID_PARAMETER.

I’m beginning to think that I will have to use static/global variables to track all the open memory mappings so that I can detect and ignore partial unmappings, but I hope you have a better idea…

edit:

Since I wasn’t explicit enough above: the docs for UnMapViewOfFile do not accurately reflect the behavior of that function.

Un-mapping the whole view and remapping pieces is not a good solution because you can only suggest a base address for a new mapping, you can’t really control it. The semantics of munmap() don’t allow for a change to the base address of the still-mapped portion.

What I really need is a way to find the base address and size of a already-mapped memory area.

edit2: Now that I restate the problem that way, it looks like the VirtualQuery() function will suffice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:32:32+00:00Added an answer on May 16, 2026 at 4:32 pm

    It is quite explicit in the MSDN Library docs for UnmapViewOfFile:

    lpBaseAddress
    A pointer to the
    base address of the mapped view of a
    file that is to be unmapped. This
    value must be identical to the value
    returned by a previous call to the
    MapViewOfFile or MapViewOfFileEx
    function.

    You changing the mapping by unmapping the old one and creating a new one. Unmapping bits and pieces isn’t well supported, nor would it have any useful side-effects from a memory management point of view. You don’t want to risk getting the address space fragmented.

    You’ll have to do this differently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which I'm currently using to change the static-IP of a
I have some code which takes strings representing hexadecimal numbers - hex colors, actually
I have some code which I want to document with in-body comments like so:
I have some code which populates a hashtable with a question as the key
I have some code which handles data files and reports an error when it
We have some code which sorts a list of addresses based on the distance
I have some code which is built both on Windows and Linux. Linux at
I have some code which builds an array of date ranges. I then call
I have some code which I think has extra release statements. Is the code
I have some code which is been running by a backgroundworker I'd like some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.