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 8804145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:31:43+00:00 2026-06-14T01:31:43+00:00

I have a C mex file here that executes properly, but crashes MATLAB with

  • 0

I have a C mex file here that executes properly, but crashes MATLAB with a segfault after it’s done executing. Since it’s crashing after the program has finished executing, it makes me think that MATLAB’s auto-freeing of allocated memory is causing the issue. However, I free my own dynamically allocated variables and do not attempt to free mxArray structures. Please have a look and see if you can help me determine why it’s causing MATLAB to crash.

#include <mex.h>
#include <matrix.h>
#include <stdio.h>

void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{ 
    /* Declare variables. */
    int i ,j, numdims, *ipos, count;
    const mwSize *dim_array;
    mwSize size;
    double *pos, rad;

    /* Ingest inputs. */
    numdims = mxGetNumberOfDimensions(prhs[0]);
    dim_array = mxGetDimensions(prhs[0]);
    rad = mxGetScalar(prhs[1]);
    pos = mxGetData(prhs[0]);
    size = dim_array[0]*dim_array[1];

    ipos = (int*) mxMalloc(size);
    for (i = 0; i < size; i++)
        ipos[i] = (int) (pos[i]*rad);
    count = 0;
    for (i = 0; i < size; i+=2)
        for (j = i + 2; j < size; j+=2)
            if (ipos[i] == ipos[j])
                if (ipos[i+1] == ipos[j+1])
                    count++;
    mxFree(ipos);

    /* Generate output */
    plhs[0] = mxCreateDoubleScalar(count);
    printf("\nProgram finished executing!\n");
}

Thanks in advance for any help!

Edit: I should also note that the inputs to the program are intended to be (in order) a 2 x n matrix and a scalar, where n can be any positive integer. The actual program checks the dimensions correctly, but I did not include those lines here to save codespace.

  • 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-14T01:31:45+00:00Added an answer on June 14, 2026 at 1:31 am

    After having written a couple hundred C programs with dynamic memory allocation, I’m a little bit ashamed to say that the mistake was this:

    Replace

        ipos = (int*) mxMalloc(size);
    

    with

        ipos = (int*) mxMalloc(size*sizeof(int));
    

    It seems that it’s always the silliest of blunders that evade me. I’d like to thank everybody who might have taken a minute or two to look at the post.

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

Sidebar

Related Questions

I have a C++ file that: starts the matlab engine calls matlab_optimize() (a compiled
I'm currently writing a MEX file in MATLAB that uses the CPLEX API in
I am trying to create a mex file that interfaces MATLAB with an external
I have a mex file (compiled in VS2010, Matlab 2010b) which accepts a variable,
I have a mex file but not the C code it was compiled from.
I am writing a Matlab mex-file. However, mex-files seem to have a serious limitation:
Is there a way to get the mex file for a built-in MATLAB m-
Hey there, I have a mex file which I want to parallelize with the
Hey there, assuming I have a compiled mex file with the name 'myMx', is
I am writing a mex-file (using C++) that will accept a memory address as

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.