Solved

putting objects into shared memory regions

Posted on 2006-11-25
1
315 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now