shared memory and multidimensional arrays...

O

oopsatwork

Ok...so, I have been outside of the C world for a _very_ long
time...but not so long as to remember how to do multidimensional
arrays. So, let me state that I know how to malloc pointers to
pointers and then malloc my data space. This question is NOT about
that.

I need to keep a dynamically sized (a result of command line options)
matrix in a shared memory segment created via shmget and attached via
shmat. My first instinct was to do what I would do if I were using
malloc. However, the docs for shm.h tell me that it is not safe to
share my pointers, as the shared memory may be attached at a different
point on different processes. So, my question is, whether or not
there is a "standard", or at least common, way of doing shared
multidimensional arrays. I could always "simulate" a multidimensional
array in a single dim. array, but that seems less than ideal. Any
thoughts?

Thanks,
-mike
 
O

oopsatwork

That should have said..."not so long as to _fail_ to remember how to
do multidimensional arrays."

my bad... ;)

-mike
 
P

ppi

point on different processes. So, my question is, whether or not
there is a "standard", or at least common, way of doing shared
multidimensional arrays. I could always "simulate" a multidimensional
array in a single dim. array, but that seems less than ideal. Any
thoughts?

You do not have many options here. You'd have better luck in
comp.unix.programmer.

The shared memory segment is one big chunk of memory mapped to your
current address space, it does not carry any more information than
that. Your code will be the one responsible to make this big piece of
memory meaningful.
As you said, pointers cannot be shared (since they are tied to each
process address space and hence likely to be different for every
process) as such, each process that maps this shared memory segment
will be responsible for indexing it for further processing.
By sharing the number of lines and columns of the memory segment plus
its data, each of your processes will be able to index it correctly.
You should be able to navigate through this memory chunk pretty easily
with the lines/columns you shared with it. The only trick is to
actually make sure these infos (meta-datas) are stored with it (like a
fixed-size header).

If you have more questions about it, ask them on comp.unix.programmer.

Cheers,
Paulo
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top