Phil
Phil

Reputation: 758

Is Linux shared memory between C and C++ feasible?

I wrote a pure-data plugin/external, but the API is in C so I want to write most of it in C++ instead (it's 2016, right?).

So I thought I could write the main core in C++ and have the pluggn only send/receive information to/from that core program.

First I though about sockets, would be easier to implement, but then I read about shared memory under linux.

But is it possible to do this between c and c++?

Upvotes: 1

Views: 290

Answers (2)

Smeeheey
Smeeheey

Reputation: 10336

Sharing memory between linux processes happens at the OS level, and it isn't really anything to do with the original language the program was written in. Both will use system calls to access the shared memory. As long as you then use it in 'raw' mode it doesn't really matter what language was used.

The exception is if you try to use the memory for structured data (i.e. structs or classes rather than 'raw' byte arrays). Then you have various language-specific quirks to contend with that have to be resolved (padding rules, vtable pointers and so on).

Upvotes: 3

Marcus Müller
Marcus Müller

Reputation: 36442

C calling conventions can directly be used and provided from within C++. Don't fool yourself. You don't need any "proxy" plugin in C and a main core in C++ – you can just write that plugin in C++, exposing a compatible C API.

Anyway, you can use the POSIX/linux system functions to access shared memory from within C++. It's really just the normal C function calls that you can use directly in C++ without writing any "adapters" or so.

Upvotes: 2

Related Questions