is it ok to use c++ STL containers like string inside a structure andpass in message ques?

Discussion in 'C++' started by ashjas, Feb 4, 2009.

  1. ashjas

    ashjas Guest

    Hi,

    I tried to use string containers inside a structure and pass it in
    message queue to recieve at other end...but got different kinds of
    errors..segmentation fault..junk values etc...

    is it recommended to use stl containers for IPC?? if not so then isint
    it a drawback of STL or these containers arent designed for IPC in the
    first place...being dynamic??

    Please shed some light...

    Thanks and regards.
    ashjas, Feb 4, 2009
    #1
    1. Advertising

  2. ashjas

    Christopher Guest

    Re: is it ok to use c++ STL containers like string inside a structureand pass in message ques?

    On Feb 4, 3:59 am, ashjas <> wrote:
    > Hi,
    >
    > I tried to use string containers inside a structure and pass it in
    > message queue to recieve at other end...but got different kinds of
    > errors..segmentation fault..junk values etc...
    >
    > is it recommended to use stl containers for IPC?? if not so then isint
    > it a drawback of STL or these containers arent designed for IPC in the
    > first place...being dynamic??
    >
    > Please shed some light...
    >
    > Thanks and regards.


    It doesn't magically work by itself. It all depends on how you
    implement the IPC on both end.
    If your just casting a structure to bytes and sending it, then of
    course your gonna have problems.
    Christopher, Feb 4, 2009
    #2
    1. Advertising

  3. ashjas

    Christopher Guest

    Re: is it ok to use c++ STL containers like string inside a structureand pass in message ques?

    On Feb 4, 4:09 am, Christopher <> wrote:
    > On Feb 4, 3:59 am, ashjas <> wrote:
    >
    > > Hi,

    >
    > > I tried to use string containers inside a structure and pass it in
    > > message queue to recieve at other end...but got different kinds of
    > > errors..segmentation fault..junk values etc...

    >
    > > is it recommended to use stl containers for IPC?? if not so then isint
    > > it a drawback of STL or these containers arent designed for IPC in the
    > > first place...being dynamic??

    >
    > > Please shed some light...

    >
    > > Thanks and regards.

    >
    > It doesn't magically work by itself. It all depends on how you
    > implement the IPC on both end.
    > If your just casting a structure to bytes and sending it, then of
    > course your gonna have problems.


    Just to be more clear in as an after thought. With my limited
    experience using IPC or any kind of messaging, one usually gets raw
    data from data structures, converts it into bytes, sets its order,
    then sends it. While the receiving end receives those bytes and
    converts it back into usable data structures.

    In the case of a string, I'd send it character by character and
    recieve it the same way. After I recieved all the characters, I'd turn
    it back into a string again.

    You would usually have some kind of protocol laid out that defines
    what is sent and recieved and how to convert it back and forth.
    Christopher, Feb 4, 2009
    #3
  4. ashjas

    SG Guest

    Re: is it ok to use c++ STL containers like string inside a structureand pass in message ques?

    On 4 Feb., 10:59, ashjas <> wrote:
    > I tried to use string containers inside a structure and pass it in
    > message queue to recieve at other end...but got different kinds of
    > errors..segmentation fault..junk values etc...


    Sounds like you just copy a struct byte by byte. This won't work for
    all kinds of objects. For those it'll work we say they are "trivially
    copyable". If any of your struct's members is not trivially copyable
    then your struct won't be trivially copyble. If your case std::string
    is NOT trivially copyable. Most containers are also not trivially
    copyable. So, you require some more advanved way of (de-)
    serialization.

    I never actually tried Boost.Serialization but it looks very useful.
    Apparently it supports many "STL classes" out of the box. You just
    need to provide a simple pair of functions serialize/deserialize for
    each user-defined type that simply serialize/deserialize the members.

    http://www.boost.org/doc/libs/release/libs/serialization/

    Cheers!
    SG
    SG, Feb 4, 2009
    #4
  5. ashjas

    James Kanze Guest

    Re: is it ok to use c++ STL containers like string inside a structureand pass in message ques?

    On Feb 4, 10:59 am, ashjas <> wrote:

    > I tried to use string containers inside a structure and pass
    > it in message queue to recieve at other end...but got
    > different kinds of errors..segmentation fault..junk values
    > etc...


    > is it recommended to use stl containers for IPC?? if not so
    > then isint it a drawback of STL or these containers arent
    > designed for IPC in the first place...being dynamic??


    Anytime the data leaves the process you're running in, it must
    be serialized. There are a few exceptions if you're using
    shared memory, but that's about it.

    This is true for all data types, not just STL containers.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Feb 5, 2009
    #5
  6. ashjas

    James Kanze Guest

    Re: is it ok to use c++ STL containers like string inside a structureand pass in message ques?

    On Feb 4, 2:14 pm, SG <> wrote:
    > On 4 Feb., 10:59, ashjas <> wrote:


    > > I tried to use string containers inside a structure and pass
    > > it in message queue to recieve at other end...but got
    > > different kinds of errors..segmentation fault..junk values
    > > etc...


    > Sounds like you just copy a struct byte by byte. This won't
    > work for all kinds of objects. For those it'll work we say
    > they are "trivially copyable".


    Formally, there are no "trivially copyable" objects in C++, at
    least not in this sense. "Trivially copyable" (e.g.
    memcpy'able) only works within the program; not between
    programs.

    In practice, you can often count on bytes being 8 bits on both
    ends, so arrays of unsigned char, or even char, are "trivially
    copiable". Everything else needs to be serialized in some form
    or another.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Feb 5, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flzw
    Replies:
    8
    Views:
    778
    Daniel T.
    Aug 15, 2004
  2. Replies:
    7
    Views:
    553
    Pete Becker
    Jan 25, 2008
  3. Andrey Vul
    Replies:
    6
    Views:
    569
    James Kanze
    Oct 22, 2009
  4. Jeremy
    Replies:
    3
    Views:
    410
  5. Sebastian Mach
    Replies:
    5
    Views:
    311
Loading...

Share This Page