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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:44:56+00:00 2026-05-25T06:44:56+00:00

I have a data structure as follows: struct s1{ unsigned char* ptr;//points to an

  • 0

I have a data structure as follows:

struct s1{
unsigned char* ptr;//points to an element in the shared memory
};

struct s1* s1Arr;

And I allocated a shared memory block and the pointer to it is:

unsigned char* shm_ptr.

I have an array of s1, with the same number of elements as there are elements in the shm_ptr array I allocated.

//point all the ptr elements of the struct to the shared memory elements(parallel array)
for(i = 0; i < count; i++)
{
shm_ptr[i] = 99;
s1Arr[i].ptr = &shm_ptr[i];
printf("val=%d\n". *s1Arr[i].ptr);
}

When I go to print *s1Arr[i].ptr, it only prints i, where i is 0, 1…while it should be printing 99.
Any thoughts as to what I am doing wrong?

I know the shared memory is working as I have tried to access it from another process.

void* allocArray_shared(int elementCount, int elementByteSize, int* shmid, key_t key)
{
    printf("allocshared errno=%d\n", errno);
    //size of entire array(cols*rows* byte size + row pointers)
    int array_size = elementByteSize * elementCount;


    //Allocate enough space for all elements + row pointers
    *shmid = shmget(key, array_size, 0666 | IPC_CREAT);

    char * arr = (char*)shmat(*shmid, NULL, 0);
    if(!arr) return NULL;

    printf("allocshared end errno=%d\n", errno);

    //Return the pointer to the first row pointer
    return (void*)arr;
}

EDIT:
Found the issue..was allocating multiple shared memory segments with the same key and thus reads/writes were overlapping…ugh…

  • 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-25T06:44:56+00:00Added an answer on May 25, 2026 at 6:44 am

    I get 99 displayed 5 times with this code. I think the only change is that I removed the keyword struct from the beginning of your struct array declaration (it was struct s1 * s1Arr).

    EDIT: I put the struct keyword back in and got the same result, so I don’t really know why my results are different than yours.

    struct s1
    {
        unsigned char * ptr;  //points to an element in the shared memory
    };
    
    s1 * s1Arr = new s1[5];
    unsigned char * shm_ptr = new unsigned char[5];
    
    for(int i = 0; i < 5; i++)
    {
       shm_ptr[i] = 99;
       s1Arr[i].ptr = &shm_ptr[i];
       cout << ((int)*s1Arr[i].ptr) << "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have structure as follows: struct data {int no; string name; int id}; I
I have a data structure that represents C# code like this: class Namespace: string
I have the following data structure (a list of lists) [ ['4', '21', '1',
I have a tree data structure that is L levels deep each node has
Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that
I have to write a component that re-creates SQL Server tables (structure and data)
Let's say we have a solution with the following structure: Project.DAL - Data access
I have a speed critical multithreaded program which involves data in a tree structure.
In rpc.h, the GUID structure is declared as follows: typedef struct _GUID { DWORD
Let's say I have the following structure declaration (simple struct with no constructor). struct

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.