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

Two programs accessing the same array

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
<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

P: n/a
Victor Bazarov wrote:
Please


That's better.
Jul 23 '05 #3

P: n/a

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