Re: problems with saving strings in a shared memory segment



On 2006-10-05, nass <athanasios.silis@xxxxxxxxx> wrote:
[...]

ps. somebody told me to 'serialise' data that are to be used by other
processes or else the pointers to these data 'will mean nothing tothe
other process'... but i do not know how t serialise string data... or
anydata whatsoever..


"serialise" means avoid using pointers and take care about big-little
endians. In your case of shared memory you don't need to worry about
"endians" :) So you have only pointers to tackle. Let's take simple
example. You have structure

struct my_struct{
char * str;
int len;
} a_structure;

You do a_structure->str = "test string" and decide to store this
structure in the shared memory. Since there's char * within the
structure, you have problem. To store this structure in shared memory
you have to convert it to something like
struct my_serial{
int len;
char str[0];
} ;

(I'm using GCC extension for declaring char str[0]!)
Now in shared memory you obtain space long enough for struct my_serial
and for the "test string", now you copy into that space you structure
and pointer like this

struct my_serial * shared = get_shared_space(sizeof(*shared) +
strlen(a_structure->str) + 1);
shared->len = a_structure->len;
strcpy(shared->str, a_structure->str);

Whoever accesses the structure in the shared memory may convert the
struct my_serial back to struct my_struct, if it's needed.

Well, the above is supposed to be very simple example :) It does not
take care about alignment things and other stuff :)

Hm, with all the above written, I still would recommend for you to spend
a little more time on learning to program. You don't understand very
important concepts of memory usage. Maybe looking at some simple
Assembler stuff will clarify things for you.

--
Minds, like parachutes, function best when open
.



Relevant Pages

  • Re: data structures on shared memory?
    ... > I've got a bit of a complex data structure based on linked lists, ... If the shared memory occupies the same set of virtual ... The second principal method is to give up on pointers ... and use offsets instead. ...
    (comp.unix.programmer)
  • Re: copying C structures acroos different processors
    ... If I need to copy this structure to shared memory if I calculated ... rather than absolute pointers. ... int FOO_GetInt16BE; ...
    (comp.lang.c)
  • Re: Creating Generic Class To Handle Shared Memory
    ... > You must write your own allocator, and make sure that nothing, absolutely nothing, can ... really difficult pieces (allocation and based pointers). ... > which won't work in shared memory; RTTI may present a similar problem, ... >>API functions required for it. ...
    (microsoft.public.vc.mfc)
  • Re: Sharing a struct/class/pointer in the data_seg() of a dll?
    ... You can use shared memory, containing a struct or array of plain-old-data, ... CreateFileMapping and MapViewOfFile. ... Shared data cannot contain any pointers to *non-shared* addresses, ...
    (microsoft.public.vc.mfc)
  • Re: How to write a structure that has pointers in the shared memory?
    ... int requestType; ... const void ** parm could further contain array of any other structure ... are going to write these structures in the shared memory and parent ... I have learned that shared memory should never contain pointers ...
    (comp.unix.programmer)