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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:15:35+00:00 2026-06-18T20:15:35+00:00

I have to memory map a file if there is not already one for

  • 0

I have to memory map a file if there is not already one for that file. The code I provided is not working. I am calling this function twice from another file and each time it is creating different mmapPtr.

char *mmapPtr;

void MemoryMapFile()
{

    int fd;
    struct stat sbuf;
    if(mmapPtr==NULL) <--- why is this executed when I called MemoryMapFile() second time
    {
        // get file descriptor of file
        if ((fd = open("example.c", O_RDONLY)) == -1) 
        {
            perror("open");
            exit(1);
        }
        if (stat("example.c", &sbuf) == -1) 
        {
            perror("stat");
            exit(1);
    }
        if ((data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==  (caddr_t)(-1)) 
        {
            perror("mmap");
            exit(1);
        }
        printf("mmap pointer %p \n",mmapPtr);
    }
  • 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-06-18T20:15:36+00:00Added an answer on June 18, 2026 at 8:15 pm

    If char* mmapPtr is not a global or static pointer the line

    if(mmapPtr==NULL) 
    

    never returns true as an uninitialized pointer has a random value, not a NULL value.

    If you declare

    char *mmapPtr = NULL;
    

    it should work,( ignoring possible other errors)

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

Sidebar

Related Questions

I have a file which I memory-map with NSData. I then delete this file
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
I need to have two processes share information through a memory mapped file. One
Problem: find ids that are in one file but not in another. Each file
Let's say I have a grammar file with this code. // START:members @header {
Two Windows processes have memory mapped the same shared file. If the file consists
I have a memory leak on one of the part of my node.js application.
I have a memory block that is divided into a series of location that
I have vague memory of a screenshot of a VS plugin that colors different
I can't see any memory change in my .map file when I try to

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.