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

  • Home
  • SEARCH
  • 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 8262053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:37:56+00:00 2026-06-08T03:37:56+00:00

I try to use shared memory with shm_open and mmap . However, whenever I

  • 0

I try to use shared memory with shm_open and mmap. However, whenever I try to write to that memory, I get bus error. The minimalist example code is given below. What is the problem here and how can it be solved?

#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

// compile with -lrt

char fname[64];
int fd;

int main()
{
    int * sm;
    sprintf( fname, "%d_%u", 4, 4 ); 

    if ((fd = shm_open(fname, O_CREAT | O_RDWR, 0777)) == -1)
    {        
        perror(NULL);
        return 0;
    }
    sm = (int*)mmap(0, (size_t)4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, 
      fd, 0);
    printf( "Now trying to see if it works!\n" );
    sm[0] = 42;
    printf( "%d, %d!\n", sm[0], sm[1] );

    return 0;
}

The output I get is the following

Now trying to see if it works!
Bus error
  • 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-08T03:37:57+00:00Added an answer on June 8, 2026 at 3:37 am

    A newly-created object has a size of zero. You cannot change the size of an object by mapping it or writing to its mapping. You probably need to call ftruncate before mmap. (If your code had error-checking, this would be much easier to figure out.)

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

Sidebar

Related Questions

In my application I open a handle to shared memory that I read/write to/from.
I try to use a variable in my kshell script but can't get a
i try to use TBXML and getting this error TBXML class method +tbXMLWithURL not
I want to read status information that an application provides via shared memory. I
I have an executable that was built in Qt. When I try to use
I am trying to reference a shared object (one that saves data), but whenever
I try use string as a regular expression pattern but I've following errors PHP
I try use a integer array in java with the code below: public static
I try to use the great particle emitter which Michael Daley build for his
I try to use SQL SERVER VIEW within RIA Services. So I created 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.