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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:07:16+00:00 2026-05-25T18:07:16+00:00

Using C, I’m trying to set up shared memory. My code looks like: key_t

  • 0

Using C, I’m trying to set up shared memory. My code looks like:

key_t key = ftok("SomeString", 1);
static int *sharedval;
int shmid = shmget(key, sizeof(int), S_IRUSR | S_IWUSR); // less permissions
sharedval = (int *) shmat(shmid, NULL, 0);
*sharedval = 0;

However the second I run that last line, I get a segmentation fault. When debugging, I can print “sharedval” and I get a memory address, presumably the place in memory I got. So I would assume all I have to do is use *sharedval to assess it, but apparently not. How am I supposed to read from the shared memory? A push in the right direction would be wonderful. Thanks!

Edit:

another.anon.coward’s output:

$ ./a.out 
ftok: No such file or directory
shmget: No such file or directory
Trying shmget with IPC_CREAT
shmget success
shmat success
Segmentation fault: 11
  • 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-25T18:07:17+00:00Added an answer on May 25, 2026 at 6:07 pm

    The problem in your case could be that there is no associated memory segment for the given key. You need to create a memory segment by passing IPC_CREAT flag in shmget in that case. Please use perror to check your error message. Use can try the following code

        #include <stdio.h> //For perror
        ...
    
        key_t key = ftok("SomeString", 1);
        if ( 0 > key )
        {
           perror("ftok"); /*Displays the error message*/
           /*Error handling. Return with some error code*/
        }
        else /* This is not needed, just for success message*/
        {
           printf("ftok success\n");
        }
    
        static int *sharedval;
        int shmid = shmget(key, sizeof(int), S_IRUSR | S_IWUSR | IPC_CREAT);
        if ( 0 > shmid )
        {
            perror("shmget"); /*Displays the error message*/
        }
        else /* This is not needed, just for success message*/
        {
           printf("shmget success\n");
        }
    
        sharedval = (int *) shmat(shmid, NULL, 0);
        if ( 0 > sharedval )
        {
           perror("shmat"); /*Displays the error message*/
           /*Error handling. Return with some error code*/
        }
        else /* This is not needed, just for success message*/
        {
           printf("shmat success\n");
        }
        *sharedval = 0;
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using XSLT 1.0, I'm trying to essentially create a small node set and then
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using JDeveloper , I started developing a set of web pages for a project
Using PHP I'd like to compare an actual ip address to part of one,
Using PHP, I'm trying to give each specific text its own variable. I believe
Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
Using a site like StackOverflow as an example: I have a table of users,
Using gcc 4.4.3 c89: I am just working on the following code below: I
Using VB6 & Crystal Report 9. Need help with the VB6 code. When i

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.