468,765 Members | 1,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Two programs accessing the same array

I'm a beginner to C++ (I've worked with Java) and I need a C program to
get an array created by another C++ program. How should I do this?
Would shared memory work to share the pointer? Can anyone give me an
example of code? Thanks.

Jul 23 '05 #1
3 1249
<gr********@hotmail.com> wrote...
I'm a beginner to C++ (I've worked with Java) and I need a C program to
get an array created by another C++ program. How should I do this?
Would shared memory work to share the pointer? Can anyone give me an
example of code?


Shared memory sounds about right. However, neither C nor C++ have that
mechanism. Anything related to IPC is platform- and OS-specific. Please
consider asking in a newsgroup for your OS. A list of suggested NGs is
located in the FAQ. The link to the FAQ can be found in a Welcome message.

V
Jul 23 '05 #2
Victor Bazarov wrote:
Please


That's better.
Jul 23 '05 #3

<gr********@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I'm a beginner to C++ (I've worked with Java) and I need a C program to
get an array created by another C++ program. How should I do this?
Would shared memory work to share the pointer? Can anyone give me an
example of code? Thanks.


It all depends what you mean by "share" ... if you want to pass the array to
a function written in C from within a C++ program, that is pretty easy. You
simply declare the C function within an extern C block (in the C++ program)
like so:

extern "C" {
// declaration matching C function-prototype
void my_cfunc(double *array);
}

and then you can call it with the C++ function. Since C and C++ use the same
memory layout for built-in arrays, this should "just work", barring index
overflow errors and the like. Of course you need to cross-link the object
code for the C++ and C elements, but that is straightforward with most
compilers .. check your docs.

OTOH, if what you want is to access elements of a common array without
passing the array as a parameter, then you do (probably) need to use shared
memory. Unfortunately I cannot help you with that since I have never done
it .. take Victor's advice and check out OS-specific NG's.

HTH,

Dave Moore
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Daniel Lidström | last post: by
6 posts views Thread by Chris Styles | last post: by
3 posts views Thread by mtjarrett | last post: by
38 posts views Thread by djhulme | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.