placement new and further

Discussion in 'C++' started by Dima Stopel, Aug 8, 2007.

  1. Dima Stopel

    Dima Stopel Guest

    Dear comp.lang.c++ members,

    I need to write an application that shares some object via shared memory
    with other applications.

    I use Memory-Mapped Files technique in order to share memory.

    Thus, in each process I have the *same* region of memory that is shared.

    All I need now is to put my object into this region.

    I use 'placement new' technique in order to put the object and all objects
    it creates in its constructor into this region.

    However, let's say that the object creates a hash_map in the constuctor. The
    hash_map is placed inside the region as needed, however if hash_map also
    creates some objects in *it's* constructor they won't be placed into the
    region and I have no control over it. Thus, the other applications which
    will try to call methods of my object will fail because those objects that
    hash_map creates are not inside the shared region.

    How can I ensure that the *whole* object with all it's sub-objects will be
    inside the shared region? Is it possible?

    Thank you in advance,
     
    Dima Stopel, Aug 8, 2007
    #1
    1. Advertisements

  2. * Dima Stopel:
    In general no.

    You can take control over dynamic allocation.

    However, you can't take control over static allocation.

    Instead of using shared memory directly, consider other means
    inter-process communication.

    In general those are abstractions on top of shared memory, but limited
    to more useful and safe functionality, such as Windows mailslots, files,
    RPC, Windows message queues, Windows DCOM/Automation, SOAP, etc.; it's
    very very rare that raw shared memory is really called for, unless
    you're implementing such an abstraction yourself.
     
    Alf P. Steinbach, Aug 8, 2007
    #2
    1. Advertisements

  3. I guess you're using the terms "whole object" and "sub-objects" too
    freely. Whatever a hash_map creates are not its "sub-objects". That
    said, I believe you need to look into the Allocator template argument
    any standard container has. It is unclear from your posting whether
    you already know about those.

    V
     
    Victor Bazarov, Aug 8, 2007
    #3
  4. Dima Stopel

    Dima Stopel Guest

    You are right.

    I'm not very familiar with the allocator specification.

    However, does the structure (as hash_map) requests the memory from the
    allocator for *any* object it creates (such as different helper objects it
    creates during the initialization), or does it requests memory *only* for
    inserting pairs into the structure ?

    If it is the latter then it is not very suitable for me.

    Thanks,
     
    Dima Stopel, Aug 9, 2007
    #4
  5. Dima Stopel

    Dima Stopel Guest

    Thank you,

    I'll read about the techniques you proposed.
     
    Dima Stopel, Aug 9, 2007
    #5
  6. Dima Stopel

    James Kanze Guest

    Be very, very careful about this. In most cases, if you need a
    constructor, you can't put the object in shared memory.
    The hash_map object requests all dynamic memory it uses using
    the allocator. Some of the objects it creates, however, may
    have their own constructors, which request memory using their
    allocators. Thus, a hash_map< std::string, ...,
    SharedMemAllocator > will not work, because the strings used as
    keys will not use the shared allocator.

    As a general rule, I'd go the other route, and try to design the
    objects in shared memory so that they are POD's.
     
    James Kanze, Aug 9, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.