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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:12:37+00:00 2026-05-31T14:12:37+00:00

I am implementing one half of a stream where you should be able to

  • 0

I am implementing one half of a stream where you should be able to jump X integers back or forth. On top of that when I open a file it should preserve the data in the file. In this implementation I try to use mmap. My problem is when I open a file a second time the mapped data is purely zeroes. My open method is called createStream and looks like this:

void MapperOutForward::createStream(const char* filename)
{
    this->filename = filename;
    pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU);
    if ( pFile ==  -1) 
    {
        int errsv = errno;
        cout << "MappedStreamOut createStream open error: " << strerror( errno ) << endl;
    }

    //getting file size and making the file big enough
    struct stat filestatus;
    stat( filename, &filestatus );
    if(filestatus.st_size < mapSize + 1)
    {
        lseek (pFile, mapSize + 1, SEEK_SET);
        write (pFile, "", 1);
    }
    lseek (pFile, 0, SEEK_SET);

    map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, 0);
    close (pFile);

    if ( map == (int*) -1) 
    {
        cout << "MappedStreamOut createStream mmap error: " << strerror(errno) << endl;
    }

    numberOfOffsets = 1;
    numberOfElementsInMap = 0;

    cout << "Kopirer 5 før og 4 efter i Create" << endl;
    for(int i = 0; i < numberOfElementsInMap + 5; i++)
    {
        cout << map[i] << endl;
    }
    cout << "Create numberOfOffsets:numberOfElementsInMap " << numberOfOffsets << ":" << numberOfElementsInMap << endl; 
}

The output at the end tells me that when I am done opening I am at the beginning of the file and the first 5 integers are 0.

The O_CREAT flag in the open command should only create a file if it is not existing, and I can se that the file keeps its size after I call this command a second time. man open

The PROT_WRITE | PROT_READ flags in the mmap command seems only to activate some security and not changing anything in the file. man mmap

The rest of the class is here:

#include "MapperOutForward.h"

using namespace std;

#include <cstdlib>
#include <iostream>

#include <fcntl.h>
#include <sys/stat.h>
#include <cmath>

MapperOutForward::MapperOutForward(int mapSize) //number of pages
{ 
    pageSize = getpagesize();
    this->mapSize = mapSize * pageSize; //we work in pages
    numberOfElementsInMap = 0;
    numberOfOffsets = 0;
}

MapperOutForward::~MapperOutForward() 
{ 
    munmap(map, mapSize);
}

void MapperOutForward::createStream(const char* filename)
{
    this->filename = filename;
    pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU);
    if ( pFile ==  -1) 
    {
        int errsv = errno;
        cout << "MappedStreamOut createStream open error: " << strerror( errno ) << endl;
    }

    //getting file size and making the file big enough
    struct stat filestatus;
    stat( filename, &filestatus );
    if(filestatus.st_size < mapSize + 1)
    {
        lseek (pFile, mapSize + 1, SEEK_SET);
        write (pFile, "", 1);
    }
    lseek (pFile, 0, SEEK_SET);

    map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, 0);
    close (pFile);

    if ( map == (int*) -1) 
    {
        cout << "MappedStreamOut createStream mmap error: " << strerror(errno) << endl;
    }

    numberOfOffsets = 1;
    numberOfElementsInMap = 0;

    cout << "Kopirer 5 før og 4 efter i Create" << endl;
    for(int i = 0; i < numberOfElementsInMap + 5; i++)
    {
        cout << map[i] << endl;
    }
    cout << "Create numberOfOffsets:numberOfElementsInMap " << numberOfOffsets << ":" << numberOfElementsInMap << endl; 
}

void MapperOutForward::writeNext(int* data, int numberOfElements)
{
    for(int i = 0; i < numberOfElements; i++)
    {
        writeHelper(data[i]);
    }   

    cout << "Kopirer 5 før og 4 efter i writeNext" << endl;
    for(int i = numberOfElementsInMap - 5; i < numberOfElementsInMap + 5; i++)
    {
        cout << map[i] << endl;
    }
    //cout << "WriteNext numberOfOffsets:numberOfElementsInMap " << numberOfOffsets << ":" << numberOfElementsInMap << endl;
}

void MapperOutForward::writeHelper(int data)
{
    int sizeOfInt = 4; //bytes
    if(numberOfElementsInMap >= mapSize / sizeOfInt) //We need to create the next part of the file
    {
        munmap(map, mapSize);

        pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU);

        if ( pFile ==  -1) 
        {
            std::cout<<"filename: "<<filename<<std::endl;
            std::cout<<"mapSize: "<<mapSize<<std::endl;

            int errsv = errno;
            cout << "MappedStreamOut writeNext open error: " << strerror( errno ) << endl;
        }

        numberOfOffsets++;

        //Make the file bigger 
        lseek (pFile, mapSize * numberOfOffsets, SEEK_SET);
        write (pFile, "", 1);
        lseek (pFile, 0, SEEK_SET);

        map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, mapSize * (numberOfOffsets - 1));
        close (pFile);

        if ( map == (int*) -1) 
        {
            cout << "MappedStreamOut writeNext mmap error: " << strerror(errno) << endl;
        }

        numberOfElementsInMap = 0;
    }

    map[numberOfElementsInMap] = data;
    numberOfElementsInMap++;
}

void MapperOutForward::jumpTo(int offset)
{
    int sizeOfInt = 4; //bytes
    //cout << "numberOfElementsInMap + offset " << numberOfElementsInMap + offset << endl;
    //cout << "mapSize / sizeOfInt " << mapSize / sizeOfInt << endl;
    if(numberOfElementsInMap + offset >= mapSize / sizeOfInt)
    {
        munmap(map, mapSize);

        pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU);

        if ( pFile ==  -1) 
        {
            std::cout<<"filename: "<<filename<<std::endl;
            std::cout<<"mapSize: "<<mapSize<<std::endl;

            int errsv = errno;
            cout << "MappedStreamOut writeNext open error: " << strerror( errno ) << endl;
        }

        numberOfOffsets += (int) ceil( (double) (numberOfElementsInMap + offset - (mapSize / sizeOfInt)) / (mapSize / sizeOfInt)); // rundet op ((resterende antal ints ud over mapSize) / (Antal ints i en mapsize)) = Antal offsets der skal springes

        //Make the file bigger 
        lseek (pFile, mapSize * numberOfOffsets, SEEK_SET);
        write (pFile, "", 1);
        lseek (pFile, 0, SEEK_SET);

        map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, mapSize * (numberOfOffsets - 1));
        close (pFile);

        if ( map == (int*) -1) 
        {
            cout << "MappedStreamOut writeNext mmap error: " << strerror(errno) << endl;
        }

        numberOfElementsInMap = (numberOfElementsInMap + offset - (mapSize / sizeOfInt)) % (mapSize / sizeOfInt); //(resterende antal ints ud over mapSize) % (Antal ints i en mapsize) = Antal elementer ind i offsettet
        //cout << "JumpTo offsetForward numberOfOffsets:numberOfElementsInMap " << numberOfOffsets << ":" << numberOfElementsInMap << endl;
    }
    else if(numberOfElementsInMap + offset < 0)
    {
        munmap(map, mapSize);

        pFile = open(filename, O_RDWR | O_CREAT | O_SYNC, S_IRWXG|S_IRWXO|S_IRWXU);

        if ( pFile ==  -1) 
        {
            std::cout<<"filename: "<<filename<<std::endl;
            std::cout<<"mapSize: "<<mapSize<<std::endl;

            int errsv = errno;
            cout << "MappedStreamOut writeNext open error: " << strerror( errno ) << endl;
        }

        numberOfOffsets += (int) floor( (double) (numberOfElementsInMap + offset) / (mapSize / sizeOfInt)); // rundet op ((resterende antal ints ud over mapSize) / (Antal ints i en mapsize)) = Antal offsets der skal springes

        //Make the file bigger 
        lseek (pFile, mapSize * numberOfOffsets, SEEK_SET);
        write (pFile, "", 1);
        lseek (pFile, 0, SEEK_SET);

        map = (int*) mmap (0, mapSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, pFile, mapSize * (numberOfOffsets - 1));
        close (pFile);

        if ( map == (int*) -1) 
        {
            cout << "MappedStreamOut writeNext mmap error: " << strerror(errno) << endl;
        }

        numberOfElementsInMap = (mapSize + (numberOfElementsInMap + offset)) % (mapSize / sizeOfInt); //(resterende antal ints ud over mapSize) % (Antal ints i en mapsize) = Antal elementer ind i offsettet
        //cout << "JumpTo offsetBacward numberOfOffsets:numberOfElementsInMap " << numberOfOffsets << ":" << numberOfElementsInMap << endl;
    }
    else
    {
        numberOfElementsInMap += offset;
        //cout << "JumpTo else numberOfOffsets:numberOfElementsInMap " << numberOfOffsets << ":" << numberOfElementsInMap << endl;
    }
}

void MapperOutForward::closeStream()
{
    munmap(map, mapSize);
}

And the main file is here:

#include "MapperInForward.h"
#include "MapperOutForward.h"
//#include "MapperOutBackward.h"
//#include "MapperInBackward.h"
//#include "MapperStreamFactory.h"
#include <functional>

int main( int argc, const char* argv[] )
{
/*********************Tester Buffer Stream*************************************/
    close(open((char*) "mapperTest", O_WRONLY | O_CREAT | O_SYNC | O_TRUNC, S_IRWXG|S_IRWXO|S_IRWXU));
    //BufferStreamFactory* bsf = new BufferStreamFactory(3);

    MapperOutForward* mso = new MapperOutForward(1);
    mso->createStream((char*) "mapperTest");

    int data[4000];

    for(int i = 0; i < 4000; i++)
    {
        data[i] = i + 1;
    }

    mso->writeNext(data, 4000);
    mso->closeStream();

    MapperOutForward* mso1 = new MapperOutForward(1);
    mso1->createStream((char*) "mapperTest");

    data[0] = 9999;

    mso1->jumpTo(0);

    mso1->jumpTo(100);
    mso1->writeNext(data, 1);

    mso1->jumpTo(1000);
    mso1->writeNext(data, 1);

    mso1->jumpTo(-900);
    mso1->writeNext(data, 1);

    mso1->closeStream();
}

It is all running on Ubuntu Linux and compiled with g++.

Thanks for the time used to read all this and I hope you can help me.

  • 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-31T14:12:38+00:00Added an answer on May 31, 2026 at 2:12 pm

    You probably need MAP_SHARED instead of MAP_PRIVATE or the changes won’t be written to the file, as specified by the standard.

    MAP_SHARED and MAP_PRIVATE describe the disposition of write
    references to the memory object. If MAP_SHARED is specified, write
    references shall change the underlying object. If MAP_PRIVATE is
    specified, modifications to the mapped data by the calling process
    shall be visible only to the calling process and shall not change the
    underlying object.

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

Sidebar

Related Questions

When implementing the Strategy Pattern, where does one put the code that determines which
I am implementing one application some fields are edittext in edittext clicking open mobile
So by now I'm getting the point that we should all be implementing our
When toying around with implementing FRP one thing I've found that is confusing is
I'm currently working on implementing Lua into one of the applications that I'm working
To support STL's notion of half-open ranges, we are allowed to point one-past-the-end of
When should one consider implementing Iterable<T> as opposed to having a collection as an
i am implementing one graph related app. In that when application start i am
I'm trying to learn about trees by implementing one from scratch. In this case
I might be missing something obvious here, but I'm implementing NSCopying on one of

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.