|
Home > Archive > Unix Programming > November 2006 > shm solution and question to earlier post
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
shm solution and question to earlier post
|
|
|
| Ok,
So i now know why my shared memory code didn't work. I used pointers in
my struct for the linked list, and i assigned these pointers in a
function using strdup() to copy the address of that string. Now, when
the child process returned, ofcourse those addresses wheren't valid
anymore.
Or are they? I put the addresses in the nodes members into shm , or do
they go away as well. That would at least explain the behaviour.
I see 2 options:
1. Either use pointers and malloc them first and then do a strncpy and
specifiy the size of the malloced value.
2. Use no pointers.
On question 1, how does malloc work then? If malloc returns a pointer
to the setup memory and i do this in a function, isn't that address
local as well?
Many thanks
| |
| Alef Veld 2006-11-18, 7:22 am |
| On 2006-11-18 13:15:04 +0100, atv <alef@xs4all.nl> said:
> Ok,
> So i now know why my shared memory code didn't work. I used pointers in
> my struct for the linked list, and i assigned these pointers in a
> function using strdup() to copy the address of that string. Now, when
> the child process returned, ofcourse those addresses wheren't valid
> anymore.
>
> Or are they? I put the addresses in the nodes members into shm , or do
> they go away as well. That would at least explain the behaviour.
>
> I see 2 options:
>
> 1. Either use pointers and malloc them first and then do a strncpy and
> specifiy the size of the malloced value.
> 2. Use no pointers.
>
> On question 1, how does malloc work then? If malloc returns a pointer
> to the setup memory and i do this in a function, isn't that address
> local as well?
>
> Many thanks
small update. I just noticed that strncpy or the whole family of string
copy functions _also_ return pointers. Am i missing something here?
Pointers aren't valid once they leave a function right or?
| |
| Jens Thoms Toerring 2006-11-18, 7:22 am |
| Alef Veld <alef@xs4all.nl> wrote:
> On 2006-11-18 13:15:04 +0100, atv <alef@xs4all.nl> said:
>
> small update. I just noticed that strncpy or the whole family of string
> copy functions _also_ return pointers. Am i missing something here?
> Pointers aren't valid once they leave a function right or?
Let's discuss separate issues separately. First malloc() etc. When
malloc() returns a pointer you got some memory from the system. If
you assign this pointer to a variable local to a function you can use
the pointer in the function and then free it again before you leave
the function or you must return that pointer from the function. If
you neither free the memory nor return the pointers value from the
function you created a memory leak - memory you neither can use anymore
nor free while the system still keeps it assigned to your program.
While the strncpy() function also returns a pointer, that's just
a convenience, the returned pointer is identical to the first
argument of the function - but strncpy() doesn't allocate any
memory, _you_ must supply ot with enough memory to operate on.
Now concerning how to deal with shared memory. Just, for the time
being, forget that you're dealing with shared memory. Think instead
of how you would organize things if the communication between the
two processes would be done via a file. Rather obviously it makes
no sense at all to put addresses (i.e. values of pointers) into
a file - address X in process A wouldn't mean zilch to process B.
So, if you want to transfer a string from A to B you would have to
put the whole string itself and not just a pointer into the file.
And if you would like to write a linked list into the file you
would have to replace pointers to the next elements by something
you can reconstruct the pointers from, e.g. a unique number for
each element of the linked list or an offset from the start of
the file to the place where the pointed to element is in the file.
And there's actually no difference between (at least in this
respect) using a file or shared memory for communication between
the two processes. You have to make all data you pass between them
independent of the memory layout of the porcess sending the data and
instead have to invent a method that allows the receicver to recon-
struct all the information. No pointers can't be used, they must be
replaced by sending the pointed to data in a way that the receiver
can re-create it's own version of the whole data set (using probably
completely different pointer values for that purpose).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
| |
| Alef Veld 2006-11-18, 1:17 pm |
| On 2006-11-18 13:48:27 +0100, jt@toerring.de (Jens Thoms Toerring) said:
>
> Let's discuss separate issues separately. First malloc() etc. When
> malloc() returns a pointer you got some memory from the system. If
> you assign this pointer to a variable local to a function you can use
> the pointer in the function and then free it again before you leave
> the function or you must return that pointer from the function. If
> you neither free the memory nor return the pointers value from the
> function you created a memory leak - memory you neither can use anymore
> nor free while the system still keeps it assigned to your program.
>
Thanks Jens for your reply. So if i understand correctly, when i use
shared memory (irrelevant now but just to provide you with a context),
i fork a child, get a ptr to shm (that is a ptr to a custom struct,
linked list format i made), then i do a strdup of a string to a member
of that struct (to which i got the ptr to shm) and i return from the
function, the address should still be valid right ?
rgds,
alef
| |
| Jens Thoms Toerring 2006-11-18, 7:20 pm |
| Alef Veld <alef@xs4all.nl> wrote:
> On 2006-11-18 13:48:27 +0100, jt@toerring.de (Jens Thoms Toerring) said:
> Thanks Jens for your reply. So if i understand correctly, when i use
> shared memory (irrelevant now but just to provide you with a context),
> i fork a child, get a ptr to shm (that is a ptr to a custom struct,
> linked list format i made), then i do a strdup of a string to a member
> of that struct (to which i got the ptr to shm) and i return from the
> function, the address should still be valid right ?
Sorry, but I don't understand that question. In any case, if you
want to pass a string from process A to process B via shared memory
then you must copy the complete string (i.e. all the characters it
consists of) into the shared memory region. strdup() won't help you
there since strdup() just creates a copy of the string in memory
that's not in the shared memory segment and thus just putting the
pointer you got from strdup() won't helo you - the receiving process
can't do anything with that pointer because the pointer will point to
something completely different in the this process and definitely
not the string in the memory of the sending process. Keep in mind
that the address spaces of both processes are completely independent
of each other - only the shared memory region is accessible by both
(but even the start address of the shared memory region may be
different in both processes).
Perhaps if you would post some code of what you are trying to do
it would be easier to understand your problems, at the moment
you're writing about to many things (shared memory, structures,
linked lists, pointers to strings etc.) in too terse a way to
make it possible for me to figure out what's going on. In the best
case you would post code that compiles and can be run and that
you cut down to contain just the essential points (a few comments
indicating where the problems are may also help).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
|
|
|