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

A

ashjas

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.
 
C

Christopher

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.
 
C

Christopher

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.
 
S

SG

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
 
J

James Kanze

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.
 
J

James Kanze

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top