Re: problems with saving strings in a shared memory segment
- From: Andrei Voropaev <avorop@xxxxxxx>
- Date: Fri, 6 Oct 2006 07:35:27 +0000 (UTC)
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
.
- Follow-Ups:
- References:
- Prev by Date: Re: UDP socket
- Next by Date: Undefined reference to 'main'
- Previous by thread: problems with saving strings in a shared memory segment
- Next by thread: Re: problems with saving strings in a shared memory segment
- Index(es):
Relevant Pages
|