I want to implement a memory mapped file. With reference to: http://www.boost.org/doc/libs/1_66_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file! i now understand the basics of memory mapped file and implementation. However i was not able to find a through example of usage of offset pointer with memory mapped file. Can anybody throw some light on this and provide small code snippet if possible?
Asked
Active
Viewed 1,364 times
0
-
UPDATE: void *addr = region.get_address(); This is the place where it becomes different for each process as raw pointer points to only virtual memory. Now if i have to replace this with a offset ptr, as it holds distance between objects, how to do it? – Jay Ghiya Mar 09 '18 at 07:08
-
What on earth will JSON have to do with your offset_ptr? Boost [Property Tree is _not_ a JSON library](https://stackoverflow.com/questions/49188604/is-there-a-proposal-for-a-json-library-in-boost-outside-property-tree). Offset-ptrs are a useful internal abstraction e.g. for shared-memory allocators. – sehe Mar 09 '18 at 14:11
-
My bad! i did not know that Boost Property tree does not support JSON. Have used it for ini and xml files in the past. – Jay Ghiya Mar 12 '18 at 10:33
-
Have edited and also answered it @sehe ! please give it a upvote if you feel i understood and explained clearly about offset pointers. – Jay Ghiya Mar 12 '18 at 10:51
1 Answers
3
The usage of offset pointer is only needed when you place objects having pointer objects in shared memory.
For instance: if you place the below structure in shared memory then you may not able to access the second raw pointer member in the process (who did not create it) as virtual address is different for each process.
struct example { int a, int* b, float c };
Solution - To use the structure in different processes, you can replace the second member with offset_ptr.
struct example { int a, offset_ptr b, int c };
ref:http://www.boost.org/doc/libs/1_38_0/doc/html/interprocess/offset_ptr.html

Jay Ghiya
- 424
- 5
- 16