bugGNU Octave - Bugs: bug #45319, mxCreateNumericArray with zero size

 
 

bug #45319: mxCreateNumericArray with zero size

Submitted by:  Amro <amro_octave>
Submitted on:  Sun 14 Jun 2015 09:26:11 AM UTC  
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  Closed
Release:  4.0.0 Operating System:  Microsoft Windows

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Fri 17 Jul 2015 10:54:58 AM UTC, comment #10: 

Thanks, works fine again!

Guillaume <gyom>
Thu 16 Jul 2015 07:20:24 PM UTC, comment #9: 

OK, thanks.  I checked in the following changeset:

http://hg.savannah.gnu.org/hgweb/octave/rev/b9bd8786d310

John W. Eaton <jwe>
Project Administrator
Thu 16 Jul 2015 06:55:20 PM UTC, comment #8: 

From my understanding of the MEX API in MATLAB, the same goes for most other mx functions: they dont explicitly check for NULL pointers, it is up to the caller to not pass nulls.

Amro <amro_octave>
Thu 16 Jul 2015 06:50:56 PM UTC, comment #7: 

yes, that will crash MATLAB (access violation).

Amro <amro_octave>
Thu 16 Jul 2015 06:47:56 PM UTC, comment #6: 

Oh, so I misunderstood.  It's not about dims == NULL, it's about ndims == 0?

I guess maybe there should also be a defensive check for dims == NULL in there somewhere anyway...

I'm curious though, does Matlab crash or issue an error or a warning for something like this:

mxCreateNumericArray (2, NULL, mxDOUBLE_CLASS, mxREAL);

?

John W. Eaton <jwe>
Project Administrator
Thu 16 Jul 2015 06:34:03 PM UTC, comment #5: 

Here is a test:

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    const mwSize dims[] = {4,3,2};
    plhs[0] = mxCreateNumericArray(0, dims, mxDOUBLE_CLASS, mxREAL);
    if (nlhs > 1)
        plhs[1] = mxCreateNumericArray(1, dims, mxSINGLE_CLASS, mxREAL);
    if (nlhs > 2)
        plhs[2] = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL);
    if (nlhs > 3)
        plhs[3] = mxCreateNumericArray(3, dims, mxINT32_CLASS, mxREAL);
}

In MATLAB, I get the following:

>> [x0,x1,x2,x3] = test_mex(); whos x*
  Name      Size             Bytes  Class     Attributes

  x0        0x0                  0  double
  x1        4x1                 16  single
  x2        4x3                 12  uint8
  x3        4x3x2               96  int32

Amro <amro_octave>
Thu 16 Jul 2015 06:29:06 PM UTC, comment #4: 

Does the attached change work?

(file #34454)

John W. Eaton <jwe>
Project Administrator
Thu 16 Jul 2015 06:13:56 PM UTC, comment #3: 

So I suppose the logic should be

if (! dims)
  create empty array
else if (ndims < 2)
  initialize dims[0] and dims[1] to 1

is that correct?

John W. Eaton <jwe>
Project Administrator
Thu 16 Jul 2015 06:00:29 PM UTC, comment #2: 

This changeset creates a new incompatibility with MATLAB:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  const mwSize dims[]={10};

  plhs[0] = mxCreateNumericArray(1,dims,mxUINT16_CLASS,mxREAL);
}

MATLAB returns a 10x1 array while Octave now returns a 10x0 one.

Guillaume <gyom>
Mon 15 Jun 2015 04:21:25 PM UTC, comment #1: 

Technically, all Matlab mxArray objects have two dimensions.  According to the documentation for mxCreateNumericArray (http://www.mathworks.com/help/matlab/apiref/mxcreatenumericarray.html) for the ndim argument:

Number of dimensions. If you specify a value for ndim that is less than 2, mxCreateNumericArray automatically sets the number of dimensions to 2.

So correct code for an empty matrix is to specify ndims = 2, with dims[0] = 0, dims[1] = 0.  This will work in Octave or Matlab.

I have changed Octave to be Matlab-compatible if the ndims < 2.  See this changeset (http://hg.savannah.gnu.org/hgweb/octave/rev/2691947f5409).  This will be a part of the next bug fix release 4.0.1.  Until then you can use the workaround above, or get the code from the stable branch of Octave's Mercurial repository and build Octave for yourself.

Rik <rik5>
Project Administrator
Sun 14 Jun 2015 09:26:11 AM UTC, original submission:  

Take the following MEX-function:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    plhs[0] = mxCreateNumericArray(0, NULL, mxDOUBLE_CLASS, mxREAL);
}

and run it:

>> out = my_mex_test();

In Octave, this creates a scalar array initialized to 0,
i.e size(out) = [1 1], similar to double(0)

In MATLAB, this creates an empty array,
i.e size(out) = [0 0], similar to double([])

I'm using the official Octave installer, and the latest MATLAB R2015a.

Amro <amro_octave>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #34454:  diffs.txt added by jwe (999B - text/plain)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by gyom (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by amro_octave (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-07-16 jwe Attached File- => Added diffs.txt, #34454
    2015-06-15 rik5 StatusNone => Fixed
        Open/ClosedOpen => Closed

    Back to the top


    Powered by Savane 3.5