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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:23:58+00:00 2026-06-12T08:23:58+00:00

I am trying to create a shared memory segment containing three integers and an

  • 0

I am trying to create a shared memory segment containing three integers and an array. The segment is created and a pointer is attached, but when I try to access the values of the variables (whether changing, printing, etc.) I get a segmentation fault.

Here is the code I tried:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>

#define SIZE 10

    int* shm_front;
    int* shm_end;
    int* shm_count;
    int* shm_array;
    int shm_size = 3*sizeof(int) + sizeof(int[SIZE]);

int main(int argc, char* argsv[])
{
    int shmid;

    //create shared memory segment
    if((shmid = shmget(IPC_PRIVATE, shm_size, 0644)) == -1)
    {
        printf("error in shmget");
        exit(1);
    }

    //obtain the pointer to the segment
    if((shm_front  = (int*)shmat(shmid, (void *)0, 0)) == (void *)-1)
    {
        printf("error in shmat");
        exit(1);
    }
    //move down the segment to set the other pointers
    shm_end = shm_front + 1;
    shm_count = shm_front + 2;
    shm_array = shm_front + 3;

//tests on shm
*shm_end = 10;                //gives segmentation fault
printf("\n%d", *shm_end);   //gives segmentation fault

           //clean-up
    //get rid of shared memory
    shmdt(shm_front);
    shmctl(shmid, IPC_RMID, NULL);

    printf("\n\n");
    return 0;
}

I tried accessing the shared memory by dereferencing the pointer to the struct, but got a segmentation fault each time.

Thanks everyone, no segmentation faults now.

  • 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-12T08:23:59+00:00Added an answer on June 12, 2026 at 8:23 am

    In your code, you didn’t include sys/shm.h, which should cause your compiler to spit out these:

    warning: implicit declaration of function ‘shmget’
    warning: implicit declaration of function ‘shmat’
    warning: implicit declaration of function ‘shmdt’
    warning: implicit declaration of function ‘shmctl’
    

    Casting the return value of shmat to an int * will also hide this warning:

    warning: assignment makes pointer from integer without a cast
    

    What may happen here is that since the shmat isn’t defined, the compiler will implicitly have it return an int instead of a void *. If the address returned by it doesn’t fit in a signed integer, you’ll get a integer overflow which is undefined behavior, and will most likely cause a segmentation fault for accessing an unallocated memory location.

    You shouldn’t cast the return value of a function returning void * specifically to catch those errors (and always compile with warnings enabled, too).

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

Sidebar

Related Questions

I'm trying to create a piece of shared memory that holds an array of
My program is trying to create an key on the HKLM\Software\Microsoft\Shared Tools\MSCONFIG\startupreg\test\ but instead
I'm trying to create a shared memory which will be used by several processes,
Im just trying to share an integer between two processes, but the memory segment
Im trying to create counter that using shared block memory, just look code: $i=0;
I am trying to create a shared library in linux I get the following
I'm trying to create a wx.Menu that will be shared between a popup (called
I'm trying to create a key like this _winreg.CreateKey(_winreg.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Shared Tools\\MSCONFIG\\startupreg\\test\\') and the key
Trying to create a black line in my view to separate text blocks but
I'm trying to create a Shared library (*.so) in ASM and I'm not sure

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.