MATLAB Answers

Basic question about pointer in mex function

21 views (last 30 days)
Chang seok Ma
Chang seok Ma on 17 Feb 2021
Commented: James Tursa on 17 Feb 2021
Hello,
I am getting confused with the basic concept of pointer in mex function. So I have couple of questions...
Below is an example from mathwork.
Q1.
As far as I know, in C++, when we declare double *y for the first time, y is the variable that stores address for data type double.
void arrayProduct(double x, double *y, double *z, mwSize n)
z[i] = x * y[i];
But I was wondering if z, y stores addresses not values how can we calculate like this z[i] = x * y[i]; ?
Shouldn't this be something like (*z)[i] and (*y)[i]? but then I see the message
error: subscripted value is neither array nor pointer nor vector
(*z)[i] = x * (*y)[i];
So I guess y stores the value.
Q2.
In the gateway function,
double *inMatrix; inMatrix is supposed to be a variable that stores addresses of data type double
inMatrix = mxGetPr(prhs[1]); inMatrix stores addresses of second argument of the input variable
Am I understanding this concept correctly?
Q3.
Then, 'inMatrix' is a pointer to 'A'
`y' is a pointer to 'inMatrix'
'multiplier' is a copy of 's'
'x' is a copy of 'multiplier'
Any help would be appreciated.
Thanks in advance.
#include "mex.h"
/* The computational routine */
void arrayProduct(double x, double *y, double *z, mwSize n)
{
mwSize i;
/* multiply each element y by x */
for (i=0; i<n; i++) {
z[i] = x * y[i];
}
}
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double multiplier; /* input scalar */
double *inMatrix; /* 1xN input matrix */
size_t ncols; /* size of matrix */
double *outMatrix; /* output matrix */
/* get the value of the scalar input */
multiplier = mxGetScalar(prhs[0]);
/* create a pointer to the real data in the input matrix */
inMatrix = mxGetPr(prhs[1]);
/* get dimensions of the input matrix */
ncols = mxGetN(prhs[1]);
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(1,(mwSize)ncols,mxREAL);
/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(plhs[0]);
/* call the computational routine */
arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
}
clc
clear all;
clear mex;
mex arrayProduct.c
s = 5;
A = [1.5, 2, 9];
B = arrayProduct(s,A)
  1 Comment
James Tursa
James Tursa on 17 Feb 2021
Probably the thing to do is brush up on C/C++ pointers. Maybe look through some online tutorials. E.g.,
double *z
means
z is of type "pointer to double"
z[i] is of type "double"
*z is of type "double"
and
(*z)[i]
makes no sense because you can't deference a double ... you can only dereference pointers to something.

Sign in to comment.

Accepted Answer

Jan
Jan on 17 Feb 2021
Q1. But I was wondering if z, y stores addresses not values how can we calculate like this z[i] = x * y[i]; ?
This is a question about C, not Matlab.
The [ ] operator in C means accessing the contents the pointer points to. So this is equivalent:
z[i]
*(z + i)
The first notation is just a nicer abbreviation.
Q2. inMatrix = mxGetPr(prhs[1]); inMatrix stores addresses of second argument of the input variable
Almost correct. mxGetPr replies the address of the data section of this Matlab variable. The address of the variable itself is the pointer in prhs[1].
Matlab variables contain a header, in which e.g. the dimensions of the data are stored, and a pointer to an array, which contains the actual values.
Q3. I do not understand, what is the question here. I see some correct statements.
  1 Comment
James Tursa
James Tursa on 17 Feb 2021
In fact, the compiler turns all expressions of the form z[i] into *(z+i) for compilation. And since this pointer addition is commutative, that means all of the following are equivalent expressions as far as the compiler is concerned:
z[5]
*(z+5)
*(5+z)
5[z]
Yes, as odd as it looks, that last one is valid C syntax and evaluates the same as the other three expressions, because it is turned into the 3rd expression for compilation.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!