How can I obtain the memory address for a variable in MATLAB 7.7 (R2008b)?

47 views (last 30 days)
I am using variables of different types such as doubles, structs and objects in my MATLAB code. I would like to obtain the memory location at which they are stored by MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Apr 2019
Edited: MathWorks Support Team on 18 Apr 2019
The address of a MATLAB variable can be obtained using a MEX function.
The gateway routine of a MEX function has the following signature:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
Here prhs is an array of right-hand input arguments. It is an array of pointers to type mxArray. When a variable is passed as an input argument to a MEX function, the pointer to that variable is copied into prhs.
For example, if a MEX function, 'myMexFunction', is called using one input argument as shown below:
a = 5;
myMexFunction(a);
the address of the variable 'a' is copied into prhs[0].
The following C-MEX function, "mem_address.c", illustrates how you can obtain the address of each element of a row vector of type double. This code can be expanded to operate on other data-types as well.
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *ptr;
int i;
mwSize n;
/* Function accepts only one input argument */
if(nrhs > 1)
{
mexErrMsgTxt("Only one input argument accepted\n\n");
}
/* This function works only with row vectors */
if( mxGetM(prhs[0]) > 1 )
{
mexErrMsgTxt("Only row vectors are accepted\n\n");
}
/* Get the Pointer to the Real Part of the mxArray */
ptr = mxGetPr(prhs[0]);
n = mxGetN(prhs[0]);
mexPrintf("Pointer Address of mxArray = %x\n\n", prhs[0]);
for(i = 0; i < n; i++)
{
mexPrintf("Value of Real Argument at array index %d = %f\n", i, ptr[i]);
mexPrintf("Pointer Address = %x\n\n", &ptr[i]);
}
}
You can MEX this C-file using the following command at the MATLAB prompt:
mex mem_address.c
This will generate a MEX file with an extension depending on your platform. You can run this MEX file as follows:
a = 5;
mem_address(a);
For more information about MEX-files please refer to the following:
  2 Comments
James Tursa
James Tursa on 27 Mar 2017
Note that cell and struct array elements are passed in as temporary shared-data-copies and do not point to the original mxArray. E.g., using this mex code:
/* File cell_address.c */
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("prhs[0] = %p\n",prhs[0]);
mexPrintf("mxGetCell(prhs[0],0) = %p\n",mxGetCell(prhs[0],0));
mexPrintf("prhs[1] = %p\n",prhs[1]);
mexPrintf("mxGetPr(mxGetCell(prhs[0],0)) = %p\n",mxGetPr(mxGetCell(prhs[0],0)));
mexPrintf("mxGetPr(prhs[1]) = %p\n",mxGetPr(prhs[1]));
}
you get the following result for a simple cell array with one element, a double vector:
>> mycell = {1:5}
mycell =
[1x5 double]
>> cell_address(mycell,mycell{1})
prhs[0] = 07376AB8
mxGetCell(prhs[0],0) = 0737EA78
prhs[1] = 0737FB18
mxGetPr(mxGetCell(prhs[0],0)) = 1FB63310
mxGetPr(prhs[1]) = 1FB63310
You can see that the mxArray address of the first cell obtained directly from mxGetCell does not match the mxArray address of prhs[1]. That is because a temporary shared data copy of that first cell was created at the m-file level as a result of the mycell{1} expression, and it is that address that gets passed to the mex routine. You can see that this is a shared-data-copy of that cell element because the data pointers obtained with mxGetPr match.
The same situation holds for struct variables as well.
James Tursa
James Tursa on 19 Apr 2019
Edited: James Tursa on 19 Apr 2019
UPDATE: The Answer by TMW is no longer true. As of R2015b, MATLAB passes in shared data copies or deep copies of variables into mex routines ... you no longer get the pointer to the original variable in the prhs[ ] array. The only way to get the original pointer is to hack into the mxArray and sift through the CrossLink list (there are no official API routines that can do this for you) ... and even this will not work if the mex routine was passed a deep copy.
E.g., for a 2-element vector
R2014a (Structure address matches Pointer Address of mxArray)
>> version
ans =
8.3.0.532 (R2014a)
>> computer
ans =
PCWIN64
>> format debug
>> a = 5:6
a =
Structure address = 16ddfb30
m = 1
n = 2
pr = 5c2541c0
pi = 0
5 6
>> mem_address(a)
Pointer Address of mxArray = 16ddfb30
Value of Real Argument at array index 0 = 5.000000
Pointer Address = 5c2541c0
Value of Real Argument at array index 1 = 6.000000
Pointer Address = 5c2541c8
R2016a (Structure address DOES NOT match Pointer Address of mxArray)
>> version
ans =
9.0.0.370719 (R2016a)
>> computer
ans =
PCWIN64
>> format debug
>> a = 5:6
a =
Structure address = 7da86240
m = 1
n = 2
pr = 95fb7ac0
pi = 0
5 6
>> mem_address(a)
Pointer Address of mxArray = 7db18f70
Value of Real Argument at array index 0 = 5.000000
Pointer Address = 95fb7ac0
Value of Real Argument at array index 1 = 6.000000
Pointer Address = 95fb7ac8
You can see that R2014a got the original mxArray pointer (matches the Structure address printed out by format debug), but R2016a didn't ... it got a shared data copy (data Pointer Address matches pr).
For scalar variables, you can now get a deep copy instead of a shared data copy. E.g.,
R2016a (deep copy ... data Pointer Address DOES NOT match pr)
>> s = 7
s =
Structure address = 7db189c0
m = 1
n = 1
pr = 95fc2f00
pi = 0
7
>> mem_address(s)
Pointer Address of mxArray = 1a707e40
Value of Real Argument at array index 0 = 7.000000
Pointer Address = 42ac9b80

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2008b

Community Treasure Hunt

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

Start Hunting!