What are the types of the variables you are passing in? E.g., here is a check at the beginning or mexFunction for the 1st argument:
if( !mxIsNumeric(prhs[0]) )
mexErrMsgTxt("parameter 1 missing!\n");
So 1st argument will get past this check if it is any numeric class (e.g., int32). Then you pass it here:
retrieve_heap( prhs[0], heap);
And you use it here:
void retrieve_heap( const mxArray* matptr, MaxHeap<double>* & heap){
// retrieve pointer from the MX form
double* pointer0 = mxGetPr(matptr);
// check that I actually received something
if( pointer0 == NULL )
mexErrMsgTxt("vararg{1} must be a valid priority queue pointer\n");
// convert it to "long" datatype (good for addresses)
long pointer1 = (long) pointer0[0];
// convert it to "KDTree"
heap = (MaxHeap<double>*) pointer1;
But this latter code will only work if the 1st argument is double class. It will not work for any other class, and will likely seg fault if it is a shorter class (e.g., int32).
Also, you are type punning here ... taking a double and converting it to a long and then type casting it to a pointer. That will only have a chance of working on a 32-bit system where pointers will fit entirely in the mantissa of a double ... it will have no hope of working on a 64-bit system (to do that would necessitate using a uint64 or int64).
Where is this heap address originally coming from?
Best Answer