By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

shared memory and multidimensional arrays...

P: n/a
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
Feb 19 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
That should have said..."not so long as to _fail_ to remember how to
do multidimensional arrays."

my bad... ;)

-mike
Feb 19 '08 #2

P: n/a
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
Feb 19 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.