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

  • SEARCH
  • Home
  • 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 7982729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:45:30+00:00 2026-06-04T10:45:30+00:00

I have a mex function that takes a double precision input vector, downcasts the

  • 0

I have a mex function that takes a double precision input vector, downcasts the precision to a temporary single precision vector, does some processing and then upcasts the result of the processing to double precision again.

The following simplified code example compiles and illustrates this process.

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{           
  int xM = mxGetM(prhs[0]); 
  int xN = mxGetN(prhs[0]);         
  int totalNumElements = xM*xN;       

  plhs[0] = mxCreateDoubleMatrix(xM,xN,mxREAL);

  double* out  = mxGetPr(plhs[0]); 
  double* in   = mxGetPr(prhs[0]);    

  float out32[totalNumElements]; 
  float  in32[totalNumElements]; 

  // ---------> DOWNCAST <---------
  for (int mm = 0; mm < totalNumElements; ++mm)
      in32[mm] = (float)in[mm];
  // ---------- DOWNCAST ----------

  // Do some single precision signal processing (just copy for this example)
  for (int mm = 0; mm < totalNumElements; ++mm)
      out32[mm] = in32[mm];

  // ---------> UPCAST <---------
  for (int mm = 0; mm < totalNumElements; ++mm)
      out[mm] = (double)out32[mm];
  // ---------- UPCAST ---------- 
}

On my machine, calling the compiled mex function like this works fine . .

>> x = randn(1e6,1); y=demo(x);

…but calling it like this causes Matlab to close unexpectedly

>> x = randn(1e7,1); y=demo(x);

Seeing as the crash is caused by an increase in the size of the input vector, I’m guessing that the error is due to a failed memory allocation. How can I gracefully quit to Matlab giving an error message if such an error occurs? Thanks.

  • 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-06-04T10:45:32+00:00Added an answer on June 4, 2026 at 10:45 am

    Method 1

    OK, one way to do this would be to replace the stack allocated variables with the C-style . . .

     float*  in32  =  (float*)mxCalloc(totalNumElements, sizeof(float));
     float*  out32 =  (float*)mxCalloc(totalNumElements, sizeof(float));
    

    When there is a memory error, Matlab neatly gives the following error . . .

    Error using demo Out of memory.

    Type HELP MEMORY for your options.

    No need for try-catch blocks or exceptions.

    Method 2

    Another method is to use C++ style std::exception. The stack allocated variables can be replaced with the following …

      float*  in32;
      float*  out32;
      try
      {
          in32  = new float[totalNumElements];
          out32 = new float[totalNumElements];
      }
      catch (std::exception& e)
      {
          std::string msg = std::string("Aw snap: ") + e.what();
          mexErrMsgTxt(msg.c_str());
      }
    

    When there is a memory error, Matlab neatly gives the following error . . .

    Error using demo

    Aw snap: std::bad_alloc

    Notes

    Method 1 is more compact and better integrated with the mex API. It is probably best to use method 1 in the mex gateway function and method 2 in a library that may have uses beyond just making a Matlab extension. Both these methods require you to use mxFree (mxCalloc solution) or delete (new solution) in order to fee the memory allocated on the heap for in32 and out32. I still don’t know how to detect or deal with stack overflows

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

Sidebar

Related Questions

I am currently writing a MEX function that will have to work with a
I have a double 2D image that I want to use in my UI
I have an embedded Matlab function that itself calls other embedded Matlab functions and
I am trying to create a C mex file that will multiply an input
I have a WCF service that has the contract published on a Net.Tcp MEX
I have a mex module called p.mexw64 which uses another dll called p.dll The
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have a matrix report now that has Position, Hours and Wages for a location
I have input from Form Name,Branch (text) x,y and Total are (number) fields Record
I have a mex file (compiled in VS2010, Matlab 2010b) which accepts a variable,

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.