How to share memory between C and MATLAB?

I have developed a DLL in C language. I have 3 functions and a viriable in that DLL. I was able to load the library and also call the function in matlab. I have a C application which also uses the same DLL. But if the variable(in DLL) value is changed in C application its not reflected in MATLAB and vice-versa. Can anyone tell me how memory sharing between C and MATLAB can be achieved? So that if i change the value of a variable in library using C its changed value should be reflected in MATLAB and vice-versa.

Answers (1)

Your C application and your MATLAB program are separate processes with their own separate address spaces. When each pulls in the DLL they do so within their own address spaces. Even though they are running the same "code" the address space the code is using is different for each.
The only thing on the FEX I am aware of related to this is the following submission by Joshua Dillon, which shares matrix data between two MATLAB sessions:
and this submission:

6 Comments

James,
Imagine the situation in which MATLAB is used to call into a DLL that happened to have been written in C, and imagine that by way of a parameter or by way of a return value, MATLAB has gained access to data whose content was changed (or possibly created) in C. Now, is there then a mechanism by which when MATLAB is used to make a call into the DLL, that the C could could "push" a change to the MATLAB understanding of the variable's contents, without having to have the MATLAB routine make a specific "update this variable" call?
I suspect the answer is Yes for variables that are not shared on the MATLAB side -- that since the data block pointer of an Mx* variable can be set on the C side, that as long as the C side keeps a record of what the data block pointer was, having the C side write relative to that pointer should have the effect of changing MATLAB's version of the variable without going through a formal pass/return cycle.
Walter,
Not sure we are talking about the same thing. The simplest example I can think of is a mex routine that keeps track of a global counter. Every time you call the mex routine it increments the counter. E.g.,
#include "mex.h"
int k = 0;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("k = %d , incremented to %d\n",k,k+1);
k++;
}
When you compile this into a mex routine it is a dll that MATLAB will load at runtime when you call the function. I can call this several times to get the global counter to icrement to say 10. If I start another MATLAB session and call the same mex routine (while the first one is still running), it will show a 0 value at the start and then increment on its own. I.e., the address space is different ... it knows nothing about the address space of the first MATLAB session.
This is the default behavior I am referring to when two separate processes attach the same dll. They may be running the same "code", but the address space the dll is running under is different for each process. There is no communication between the two.
Getting two different processes to communicate on purpose is beyond my area of expertise, but I am aware of the work that Joshua Dillon has done on this subject using POSIX and the Boost InterProcess Library techniques, so I pointed ag in that direction. If ag has questions about how those submissions work I will not be able to answer them.
Two different mex routines in the same MATLAB session can access the same dll to communicate with each other, but that is all within the same process (hence the same address space) and it was not my impression that ag was talking about that scenario.
ag
ag on 14 Jun 2013
Edited: ag on 14 Jun 2013
Hello
I want to know if its possible to share memory between C application and MATLAB. As in if i change a variable value present in a DLL in C,its changed value should be reflected in MATLAB and vice-versa. I have seen the link given by James. But it talks about using same function by 2 MATLAB sessions. I am talking about MATLAB and C application Here is the header file of my DLL:
__declspec(dllexport) int sum(int x,int y);
__declspec(dllexport) int diff(int i,int j);
__declspec(dllexport) void get(int x);
__declspec(dllexport) int varia;
Now if i change the value of variable "varia" using C application and then load the DLL into matlab using loadlibrary n then call the function sum, (which adds 2 of its arguments to the variable "varia" and returns the sum) the expected result is not obtained.The function sum is :
int sum(int x,int y)
{
int p=0;
p=x+y+varia;
return p;
}
They are not the same because your C application and MATLAB are two different processes with different address spaces. The two different processes are not talking to each other. The int varia in the dll attached to MATLAB is different from the int varia in the dll attached to your C application.
ag
ag on 14 Jun 2013
Edited: ag on 14 Jun 2013
Ok.So there is no way to achieve what i m trying to achieve? Or is there any other way to make MATLAB and C code to share a common piece of memory?
James Tursa
James Tursa on 15 Jun 2013
Edited: James Tursa on 15 Jun 2013
The easy way to do this is to turn your C application into a mex function. I.e., load it as a dll into MATLAB. That way it will share the same address space as MATLAB and can access the same shared memory. The harder way is using something like Joshua Dillon's submission noted above.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Asked:

ag
on 12 Jun 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!