Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

putting objects into shared memory regions

Posted on 2006-11-25
1
342 Views
Last Modified: 2010-04-01
Using the shmget and shmat function you can allocate a block of memory that multiple processes can access.  But suppose you want to store an object in that memory.  So you copy the object to the shared memory using memcpy, and then any process can access it with an object* pointer.  

This seems like it would work fine.  But what if the object itself contains an internal pointer to another memory location?

For example, suppose you have:

class Box {
     char* p;

     public:
     Box() { p = new char[100]; }
     ~Box() { delete[] p; }
     char* data() { return p; }
};

And then you do:

Box box;
void* sharedmem = shmat(shmid, (void*) 0, 0);
memcpy (sharedmem, &box, sizeof(box));

Now a copy of the object "box" is in the shared memory region.  So, I set a pointer to it like:
Box* pbox = (Box*) sharedmem;

And then I can access the data in Box by doing:
pbox->data();

But wouldn't this cause a segmentation violation if another process tried to access the data in box?  Because even though box itself is stored in the shared memory region, the data pointed to by the member variable of box is NOT in the shared memory region, and is in fact, part of another process.  So wouldn't this be an unsafe thing to do?  If so, is there any possible way to share objects between processes?
0
Comment
Question by:chsalvia
1 Comment
 
LVL 15

Accepted Solution

by:
efn earned 250 total points
ID: 18013292
You're right, that would not be safe.  However, a self-contained object in shared memory could be shared between processes.  Or an object that was not self-contained could be shared if you were careful.  The memory not contained in the object would have to be in some shared memory area, and you would have to manage it yourself, using offsets from the beginning of the shared memory area.  That is, the usual malloc or new facilities won't manage a shared memory area for you, and its address will not necessarily be the same in all processes that use it, so pointers within it are not portable across processes, and you will have to use offsets from the beginning of the area to identify locations portably.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question