sharing variables

how to share an array by using shared memory..?
shmget() will return a void * .but how to share an array between two processes
You will need to cast the void* to the variable type in the array.

For example, if your array is

double[100] foo;

and you share that with another process, that other process has to know to cast the void* to a double*.

// In your other process...
double* foo = reinterpret_cast<double*>(shmat(...));

And, because pointers can be used as arrays, the other process can use it's double pointer like an array:

double x = foo[32];

Does that help?

I generally find it easier to use message queues (man mq_open to get started) than shared memory for interprocess communication. You don't have to worry about semaphores to control who can read and write to the shared memory, but shared memory has its place.
PanGalactic:

why would he need to use reinterpret_cast to cast a void* to char* ???
a normal cast will do.

By "normal cast" do you mean static_cast or old, C-style cast?

I'm casting a void* to a double* -- and that can be done with a static_cast.

I've gotten in the habit of using reinterpret_cast when casting from one unrelated pointer type to another. But you are right. It's not necessary.
The intention of C++ style casts is not only to perform the cast (since that can usually be done sufficiently with C-style casting), but also to make your intent more clear. Often (and this case is a good example), there are several different casts available, all of which have the same result. The only difference is how they appear in your code, and how they impact readability.

Personally, I tend to use reinterpret_cast for converting to/from void* (though I try to refrain from using void* completely, wherever possible due to the obvious type ambiguities). I choose to due this simply because the alternatives make less sense in my mind:

C-style cast: totally ambiguous. I refrain from using it in all my C++ code since it is less likely to throw a compiler error on a truely bad cast, and because the end result of it is not obvious (does it reinterpret cast? hierarchy-aware up/down cast? const cast?)

static_cast: static_cast is compile time type aware, so using it to cast to/from void* is a little strange to me because you can't really be type-aware from void*.

So whatever, it's a matter of personal preference. In any event, I'd recommend avoiding C style casts.
Last edited on
Great explanation, Disch.
PanGalactic:

i mean c-style caste. primitive data types can be casted using c style casts. it gives better performance also.



in normal circumstances reinterpret cast should not be used. it will give you no guarantees.. it can cast from anything to anything but wont tell you if you are in trouble. :P

it should be the last choice. one good thing about it is casted pointer can be casted back to original.
They are abnormal casts in C++. Best practice is to not use C-style casts in C++. Read "C++ Coding Standards" by Sutter and Alexandrescu for why that is. (Our group forbids them in our code.)

And, yes, reinterpret_cast should be avoided -- but only in the sense that code constructs that require it should be avoided. They must be used when they are required.

Again, in this case, static_cast will suffice.
i mean c-style caste. primitive data types can be casted using c style casts.


This is true, but see points before of why C-style casts are 'bad' (tm)

it gives better performance also.


I think you are mistaken. The only C++ cast which might incur more overhead than a C-style cast is dynamic_cast because it does runtime checks. All the other casts are type-resolved at compile time, and are therefore just as quick as C-style casts.

in normal circumstances reinterpret cast should not be used. it will give you no guarantees


It's guaranteed not to change the original binary data being casted, whereas all other types of casts might. That's the whole point of it. Again with void pointers there isn't much difference because they're already fundamentally type unsafe, but you can use reinterpret_cast to state your intent more clearly.

Though now that I think about it more static_cast would probably be better (in case the void pointer changes to a typed pointer in the future). So yeah I retract my earlier reinterpret_cast recommendataion.

Anyway, if anything has no guarantees, it's C-style casting. C-style casting can do any one of 4 different types of casts, and it silences the compiler, preventing it from warning you that you're screwing something up. It's up to you to be sure you're getting the type of cast you want, and that the type of cast is legal/possible. Be 100% sure or else prepare to get slews of hard-to-find runtime bugs.


But again it's all about preference. If you like C-style casts for this job, then fine. In this case... static_cast, C-style cast, and reinterpret_cast all have the same end result. It's all about which you prefer to use.

Also +1 to what PanGalactic said.

*minor edit* - changed some stuff to sound less like a jerk. (sorry)
Last edited on
*minor edit* - changed some stuff to sound less like a jerk. (sorry)

hahahahaha... thats fine...


Best practice is to not use C-style casts in C++

no comments!!!

ok..so we conclude the discussion:

it depend on personal preference.... :P ;)
Topic archived. No new replies allowed.