Two programs accessing the same array

G

greyham433

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.
 
V

Victor Bazarov

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
 
D

Dave Moore

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
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top