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 6225507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:51:18+00:00 2026-05-24T08:51:18+00:00

I have allocated code using mmap , but unable to free it because of

  • 0

I have allocated code using mmap, but unable to free it because of segmentation fault. I have done mprotect – PROT_WRITE to make it writable, but still, I am unable to free it. My code:

 1 #include <stdio.h>
 2 #include <memory.h>
 3 #include <stdlib.h>
 4 #include <unistd.h>
 5 #include <sys/mman.h>
 6 #include <sys/types.h>
 7 #include <fcntl.h>
 8 
 9 int main()
10 {
11  void * allocation;
12  size_t size;
13  static int devZerofd = -1;
14 
15  if ( devZerofd == -1 ) {
16                 devZerofd = open("/dev/zero", O_RDWR);
17                 if ( devZerofd < 0 )
18                         perror("open() on /dev/zero failed");
19 }
20 
21  allocation = (caddr_t) mmap(0, 5000, PROT_READ|PROT_NONE, MAP_PRIVATE, devZerofd,  0);
22 
23  if ( allocation == (caddr_t)-1 )
24                 fprintf(stderr, "mmap() failed ");
25 
26  if ( mprotect((caddr_t)allocation, 5000, PROT_WRITE) < 0 )
27         fprintf(stderr, "mprotect failed");
28  else
29         printf("mprotect done: memory allocated at address %u\n",allocation);
30 
31  strcpy(allocation,"Hello, how are you");
32  puts(allocation);
33 
34  if ( mprotect((caddr_t)allocation, 5000, PROT_WRITE) < 0 )
35         fprintf(stderr, "mprotect failed");
36 
37  free(allocation);
38 
39 }
40 
41 
  • 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-24T08:51:20+00:00Added an answer on May 24, 2026 at 8:51 am

    You need to use munmap for that. You don’t need to do anything else (change protection bits etc). But you should check the return code of munmap.

    munmap(allocation, 5000);
    

    free(3) can only be used to free memory allocated via malloc(3).

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

Sidebar

Related Questions

So I have the following code, which allocates memory using the mmap linux system
I have written a segmentation fault handler, using sigsetjmp and siglongjmp. Once it goes
I have been trying to free memory allocated via malloc() using free() . Some
I have a circular, statically allocated buffer in C, which I'm using as a
I have one C code app. which i was building using MS-VS2005. I had
Basically, I have a block of memory allocated using malloc that I want to
I have a chunk of code that I'm using to get the UNC path
I have trouble deallocating memory that I allocated using malloc. The program runs fine
I have a CryptoAPI code to encrypt\ decrypt given data using AES-128 and a
I have the following code which you can try using c99 filename.c; ./a.out #include

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.