Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6322155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:16:43+00:00 2026-05-24T16:16:43+00:00

I am writing a mex-file (using C++) that will accept a memory address as

  • 0

I am writing a mex-file (using C++) that will accept a memory address as an input, and operate on data at that memory address. Because I am forced to use MATLAB as my environment, my program can only accept MATLAB data types as inputs (char, bool, float, double, and int). How can I go about assigning my input value to a pointer?

Pseudocode:

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

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

Basically, this can be seen as me hardcoding the value of a pointer similar to:

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

I am receiving the error:

error C2440: ‘=’ : cannot convert from ‘double’ to ‘double *’

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:16:43+00:00Added an answer on May 24, 2026 at 4:16 pm

    What you’re trying to do is dangerous because you’re passing raw pointers to some C++ code, making assumptions about what exists at that location and executing code based on those assumptions. But let’s say you’ve taken care of all those security issues.

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

    On the MATLAB side:

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

    Within 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 a double as well, just change the mxGetClassID check to look for mxDOUBLE_CLASS. In this case the cast expression becomes:

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

    EDIT:
    What I’ve said about being about to get this code to work on a 32 or 64-bit machine is only true if both machines are little endian. Otherwise, if you pass a 32-bit pointer in a 64-bit uint on a big-endian machine and cast it to a double * you’ll get a pointer that is 0x00000000.

    You can handle the endianness issues by using the MATLAB function computer to detect machine endianness. If byte swapping is needed you can use swapbytes. To execute these functions from your C++ code use mexCallMATLABWithTrap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I writing a report in Visual Studio that takes a user input parameter and
I am writing a Matlab mex-file. However, mex-files seem to have a serious limitation:
Writing a custom validator for a dropdownlist that is using autopostback. Seems to ignore
Writing Scala code, I regularly encounter cases where I have processor functions that operate
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
When writing production-quality VC++ code, is the use of recursion acceptable? Why or why
I´m writing an application for Android in which I have a character that I
When writing a Perl module, is it a good practice to use croak/die inside
My situation is the following: I am writing a toolbox that generates two libraries.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.