Explicit pointer value definition

I am writing a mex file (using C ++) that will take a memory address as input and work with data at that memory address. Since I have to use MATLAB as an environment, my program can only accept MATLAB data types as input (char, bool, float, double and int). How can I assign my input value to a pointer?

pseudo code:

// Outside of program // double input_arg = hex2dec('00C2E4E8') double *pointer; pointer = (double *)input_arg; // pointer == hex2dec('00C2E4E8') 

Basically, this can be considered as a hardcoding pointer value, similar to:

 double *pointer = (double *)hex2dec('00C2E4E8'); 

I get an error message:

error C2440: '=': cannot be converted from 'double' to 'double *'

I also tried using static / const / reinterpret / dynamic_cast, but I really don't understand how they work (and I couldn't get them to work). Is it even possible to assign memory address values ​​for pointers manually?

+7
source share
4 answers

What you are trying to do is dangerous because you pass the source pointers to some C ++ code, making assumptions about what exists in this place, and execute the code based on these assumptions. But let me say that you took care of all these security issues.

You will probably want to pass the pointer to your MEX file as UINT64 (so that the code can be recompiled and used, as is done with the 64-bit MATLAB installation).

On the MATLAB side:

 ptrArg = uint64(hex2dec( '00C2E4E8' )); myMexFile( ptrArg ); 

In your mex function:

 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if( nrhs < 1 ) { // error } else { // Verify that the address argument is a UINT64 if( mxGetClassID( prhs[0] ) != mxUINT64_CLASS ) { // error } } // Done with error checking, now perform the cast uint64_T mlData = *static_cast<unsigned long long *>( mxGetData( prhs[0] ) ); double *p = reinterpret_cast<double *>( mlData ); // Do whatever with p } 

Note. You can do the same with double, just change the mxGetClassID check to find mxDOUBLE_CLASS . In this case, the expression cast will look like this:

 double *p = reinterpret_cast<double *>( *mxGetPr( prhs[0] ) ); 

EDIT:
What I said about making this code work on a 32-bit or 64-bit machine is true only if both machines are not oriented much. Otherwise, if you pass the 32-bit pointer to a 64-bit uint on the big-end machine and pass it to double * , you will get a pointer 0x00000000 .

You can handle content issues using the MATLAB computer function to determine the limb of a machine. If byte exchange is required, you can use swapbytes . To perform these functions from your C ++ code, use mexCallMATLABWithTrap .

+5
source

It seems that you cannot use double , which is a floating-point type, as a representation of an address in memory, which is essentially an integer value.

I assume that your input_arg should be defined as a MATLAB int type, not a double .

+6
source

Try:

double *pointer = (double *)0x00C2E4E8;

(add 0x)

+5
source

Why is input_arg double? Pointers are always of integer type.

This may solve the "double to double * conversion"

+5
source

All Articles