2011-06-18 43 views
1

boost :: interprocess :: managed_(windows_)shared_memory :: constructを使用して、メンバー変数を持つ独自のクラスを持つプロセス間ベクトルを構築しましたその型のstd :: stringと型のstdの別::ベクトル、の:boost :: interprocess - std :: string vs std :: vector

私はテストのためにこれをしなかった
class myclass 
{ 
    public: 
     myclass() 
     { 

     } 

     std::string _mystring; 
     std::vector <int> _myintvector; 
}; 

template < class _type > 
struct typedefs 
{ 
    typedef boost::interprocess::managed_windows_shared_memory  _memory; 
    typedef _memory::segment_manager        _manager; 
    typedef boost::interprocess::allocator < _type, _manager >  _allocator; 
    typedef boost::interprocess::vector < _type, _allocator >  _vector; 
}; 

typedef typedefs <myclass> tdmyclass; 

int main() 
{ 
    using namespace boost::interprocess; 

    managed_windows_shared_memory mem (open_or_create, "mysharedmemory", 65536); 
    tdmyclass::_vector * vec = mem.construct <tdmyclass::_vector> ("mysharedvector") (mem.get_segment_manager()); 
    myclass mytemp; 

    mytemp._mystring = "something"; 
    mytemp._myintvector.push_back (100); 
    mytemp._myintvector.push_back (200); 

    vec->push_back (mytemp); 

    /* waiting for the memory to be read is not what this is about, 
    so just imagine the programm stops here until everything we want to do is done */ 
} 

は、私はどちらのstd ::文字列ものstd ::ベクトルがあれば、作業、まだすることが期待しました私は別のプロセスから読み込み、std :: stringは実際に動作し、それは私が割り当てた文字列を含んでいます。それは本当に私を驚かせた。 反対側のstd :: vectorは部分的にしか機能しませんが、size()によって返される値は正しいですが、イテレータにアクセスするか演算子[]を使用するとプログラムがクラッシュします。

だから私の質問は、どうしてそうですか?つまり、実際にはVisual StudioのSDKのSTL実装を読んだことはありませんが、std :: stringだけではなく、文字列に適した特別な関数を持つstd :: vectorですか?彼らはどちらもstd :: allocatorを使いません - どちらも、std :: stringとstd :: vectorは共用メモリでは動作しませんか?

これでグーグルすると、実際にはboost :: interprocess :: vector以外の結果は得られません。それは私が検索したものではありません。だから私は誰かが私に何かについての詳細を教えてくれることを願っています^^

PS:私は上記のコードでタイプミスをしてしまったのですが、私はこのページエディタでこれを書きました。私のIDEのオートコンプリートに使用されています^

答えて

6

std::stringは、あなたの標準ライブラリ実装が小文字の最適化(SSO)を使用しているために機能します。これは、文字列"something"の値が動的メモリ割り当てなしで文字列オブジェクト自体にコピーされることを意味します。したがって、あなたは他のプロセスからそれを読むことができます。長い文字列(30文字を試してください)の場合は動作しません。

std::vector標準でSSOを使用できないため、機能しません。最初のプロセスのアドレス空間にメモリを割り当てますが、このメモリは他のプロセスからアクセスできません。 .size()は、の内部にという名前で格納されているため、動作します。

P.S. stringvectorには多くの違いがあります。最も重要なのは、少なくとも言及すると、string is not a container from standard's point of viewです。

+0

正確に。したがって、長い文字列では機能しません。それをチェックしてください:_) – sehe

+0

大変感謝しています^^私は期待したとおりにstd :: vectorが動作することを忘れていました^^しかしSSOから何も聞いたことはありません^^ – Andy

関連する問題