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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:32:17+00:00 2026-05-23T11:32:17+00:00

This problem is unfortunately narrow, but I’m at a loss. I have a custom

  • 0

This problem is unfortunately narrow, but I’m at a loss.

I have a custom mex file that takes two lists of uint32s that are each sorted and contain no common entries and returns a single sorted list containing all of the entries from both list. The code is:

#include "mex.h"
#include "matrix.h"
#include "string.h"

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

void CalculationRoutine(uint32_T* CombinedList, const mwIndex FirstNumels, uint32_T* FirstList, const mwIndex SecondNumels, uint32_T* SecondList) {
mwIndex OutCounter = 0, FirstCounter = 0, SecondCounter = 0;
unsigned int i;

// Short-circuit if there is no ovelap.
if (*FirstList > *(SecondList+SecondNumels-1)) {
    memcpy(CombinedList,SecondList,SecondNumels*sizeof(uint32_T));
    memcpy(CombinedList+SecondNumels,FirstList,FirstNumels*sizeof(uint32_T));
    return;
} else if (*SecondList > *(FirstList+FirstNumels-1)) {
    memcpy(CombinedList,FirstList,FirstNumels*sizeof(uint32_T));
    memcpy(CombinedList+FirstNumels,SecondList,SecondNumels*sizeof(uint32_T));
    return;
}

// These can be done with no exhaustion checking. Leave one item because we
// are doing post-checking in the second loop.
for (i=MIN(FirstNumels, SecondNumels)-1; i--;) {
    if (*(FirstList+FirstCounter) < *(SecondList + SecondCounter)) {
        *(CombinedList+OutCounter) = *(FirstList+FirstCounter);        
        FirstCounter++;
    } else {
        *(CombinedList+OutCounter) = *(SecondList+SecondCounter);        
        SecondCounter++;
    }
    OutCounter++;
}

// These ones need exhaustion checking.
while (1){
    if (*(FirstList+FirstCounter) < *(SecondList + SecondCounter)) {
        *(CombinedList+OutCounter) = *(FirstList+FirstCounter);        
        FirstCounter++;
        if (FirstCounter == FirstNumels) {
            // Just copy the rest of the second list.
            memcpy(CombinedList+OutCounter+1,SecondList+SecondCounter,(SecondNumels-SecondCounter+1)*sizeof(uint32_T));
            return;
        }
    } else {
        *(CombinedList+OutCounter) = *(SecondList+SecondCounter);        
        SecondCounter++;
        if (SecondCounter == SecondNumels) {
            // Just copy the rest of the first list.
            memcpy(CombinedList+OutCounter+1,FirstList+FirstCounter,(FirstNumels-FirstCounter+1)*sizeof(uint32_T));
            return;
        }
    }
    OutCounter++;
}
}

void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] ) {

mxArray *CombinedList = NULL;
uint32_T *FirstList, *SecondList;    
mwIndex FirstNumels = mxGetNumberOfElements(prhs[0]), SecondNumels  = mxGetNumberOfElements(prhs[1]);   

//Input Checking
if (!mxIsUint32(prhs[0])) {
        mexErrMsgTxt("FirstList must be matrix of uint32.");
}
if (!mxIsUint32(prhs[1])) {
        mexErrMsgTxt("SecondList must be a matrix of uint32.");
}

CombinedList = mxCreateNumericMatrix(FirstNumels+SecondNumels, 1, mxUINT32_CLASS, mxREAL);
if (CombinedList == NULL) {
    mexErrMsgTxt("SecondList must be a matrix of uint32.");
}

//Short circuit when we have one or the other inputs empty.
if (mxIsEmpty(prhs[0])){
    if (!mxIsEmpty(prhs[1])) {
        // Return the SecondList verbatim.
        //CopyOneInput(mxGetData(CombinedList),SecondNumels, mxGetData(prhs[1]));
        memcpy(mxGetData(CombinedList), mxGetData(prhs[1]),SecondNumels*sizeof(uint32_T));
    }
    plhs[0] = CombinedList;
    return;
} else if (mxIsEmpty(prhs[1])) {
    // Return the FirstList verbatim.
    //CopyOneInput(mxGetData(CombinedList),FirstNumels, mxGetData(prhs[0]));
    memcpy(mxGetData(CombinedList), mxGetData(prhs[0]),FirstNumels*sizeof(uint32_T));
    plhs[0] = CombinedList;
    return;
}

CalculationRoutine(mxGetData(CombinedList),FirstNumels,
    mxGetData(prhs[0]),SecondNumels,mxGetData(prhs[1]));

plhs[0] = CombinedList;
}

When I run code that calls the mex file, I get assertion detected errors (with stuff like Found corrupted block 381 in table 5. (invalid table index). ). The assertions always arise, but not necessarily at the same place.

If I revert to the old version of the code, there are no problems. So something is mangling memory, but I can’t see it. One of the changes I made is to use memcpy, but can’t see anything wrong there.

Again, I’m sorry that this is such a narrow question, but any help would be appreciated.

UPDATE: It is definitely the memcpy that is causing the assertion. If I revert to assigning values in a loop, the assertions stop. Are there restrictions with using memcpy in mex-files?

  • 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-23T11:32:17+00:00Added an answer on May 23, 2026 at 11:32 am

    It’s an off-by-one error in the third argument to memcpy. The while loop should be:

    while (1){
        if (*(FirstList+FirstCounter) < *(SecondList + SecondCounter)) {
            *(CombinedList+OutCounter++) = *(FirstList+FirstCounter++);
            if (FirstCounter == FirstNumels) {
                // Just copy the rest of the second list.
                memcpy(CombinedList+OutCounter,SecondList+SecondCounter,(SecondNumels-SecondCounter)*sizeof(uint32_T));
                return;
            }
        } else {
            *(CombinedList+OutCounter++) = *(SecondList+SecondCounter++); 
            if (SecondCounter == SecondNumels) {
                // Just copy the rest of the first list.
                memcpy(CombinedList+OutCounter,FirstList+FirstCounter,(FirstNumels-FirstCounter)*sizeof(uint32_T));
                return;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have posted this problem yesterday..but unfortunately, no one has given me the solution
This problem is giving me a real headache. I have two tables in my
This problem is a little similar to that solved by reservoir sampling , but
I've stuck at this problem. Our old-old programmers have encoded their CFM file based
I have looked around for a solution to this problem, but it's so wonky
This problem has been kicking my butt for a few days now. I have
This problem occurs when I try to hide a file in my directory with
This problem has bugged me so many times and i have now decided to
I looked at other questions regarding this problem and tried their solutions, but it
I have a problem loading different nib files in one tableView. I saw this

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.