-1

My original question is here.

I'd like to write an array of objects into shared memory. Let's assume we know the starting address of the shared memory. How should I store the array of objects into the shared memory and manipulate the array later(e.g.,access one particular object in the array and even the fields of that object)? Do I have to serialize the objects into the memory and implement relevant access methods myself or does C++ have memory management mechanisms to deal with the details?

Community
  • 1
  • 1
Terry Li
  • 16,870
  • 30
  • 89
  • 134
  • 1
    Could you post some code that you have tried? It seems like you have some misconceptions on things but it's hard to know without seeing your train of thought. – Pubby Nov 28 '11 at 15:45
  • You need to search for a good tutorial on how to use shared memory, you're likely to understand more that way... – Nim Nov 28 '11 at 15:48
  • 1
    Shared Memory just ensures that you have a common block of memory which is accessible across different processes.Once You have a block of memory shared it is accessible just as local storage, there is nothing special about it then after. – Alok Save Nov 28 '11 at 15:52
  • @Pubby I'm still in the design phase and haven't started coding yet. Basically my question boils down to the following: when we ask our system to create an array of objects, it allocates a contiguous block of memory of its choice and stores our objects in its own way. Now I'd like to do the same(i.e., have the same ability to access our objects) except this time I want to store the array of objects in a contiguous block of memory of our choice(e.g., shared memory). – Terry Li Nov 28 '11 at 15:56
  • 1
    @Terry LiYifeng: Shared memory isn't choosing where to allocate. Usually you access the previously shared memory by an alias. If you want to place objects in previously allocated memory, shared or otherwise, search for "placement new" – AJG85 Nov 28 '11 at 16:08
  • @AJG85 This is what I've been looking for. Thanks a lot. It deserves to be an answer! – Terry Li Nov 28 '11 at 16:12
  • @Terry LiYifeng: Nah it was just a good guess based on the wording of your previous comment. As a side note you may need to consider alignment constraints depending. See http://stackoverflow.com/questions/4011577/placement-new-array-alignment – AJG85 Nov 28 '11 at 17:03

1 Answers1

1

This isn't a particularly thought out answer, but I can't see where you're stuck since you didn't provide any code to give us a hint.

There is a sample program here - Sample Shared Memory Program - with sufficient commenting to make you understand how to achieve what you're asking though.

So, I'd say read that through carefully and give it a shot :)

John Humphreys
  • 37,047
  • 37
  • 155
  • 255