Re: Creating a logical matrix in a mex-file



Christopher Hulbert wrote:

Generate the array using one dimension of 0 or rank 0. This will
not allocate
the data for the array. Then use mxMalloc to allocate the memory
*without*
initialization. Use mxSetData to set the data of the array to the
allocated
memory. This would avoid the initialization of the memory.

Ok. I not quite sure how to do that. For example how do I
transform

int m = 1000;
int n = 1000;
mxArray *A_ptr;
A_ptr = mxCreateLogicalMatrix(m, n);
bool *A;
A = mxGetPr(A_ptr);

into code which does not initialize the elements of A.
My guess would be

int m = 1000;
int n = 1000;
mxArray *A_ptr;
A_ptr = mxMalloc(m*n+2);
mxSetM(A_ptr, m);
mxSetN(A_ptr, n);
bool *A;
A = mxGetPr(A_ptr);

I really have no idea how much memory to allocate. Since there a
differnce between a pointer to an mxArray and a pointer to the data
of an mxArray, and since arrays are stored as vectors, there should
be some extra bytes storing
the dimension and size of the array. Using mxMalloc I would guess I
have to take that into account.
Another unanswered question is whether I am chasing a ghost here
trying to avoid initialization.

Thank you all for your patience and help with my neverending
questions on mex-files.

Fred
.



Relevant Pages

  • Re: Initial values of File scoped and Block level variables
    ... the loaderallocates data segment ... If T is a function type, any initialization ... >that informs the loader to allocate 1 element of sizeofmemory. ...
    (comp.lang.c)
  • Re: Initialization of character array in derived type
    ... In fact, if the array is large enough for the time to be measurable, ... be faster than doing it with static initialization. ... I see that your init-mat_data is a pointer. ... allocate or assign the pointer somewhere. ...
    (comp.lang.fortran)
  • Re: Another C# marshaling question
    ... // Get the pointer of the location in memory. ... > public static extern int FF_Function( ... > // Allocate the array. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: INTENT(WORKSPACE) as a new Fortran feature?
    ... >> the array once in the caller than each time within the subroutine. ... > the stack pointer is being changed to allocate local variables in any ... "actual" memory would never be touched at all. ... If, on a particular machine, the compiler knows that it would be ...
    (comp.lang.fortran)
  • Re: Storing the size of an array in the structure itself
    ... >> I think every C programmer can relate to the frustrations that malloc ... >> the size of an array must be stored separately to be a nightmare. ... is anything more than just that - a chunk of memory. ... > Otherwise you couldn't tell it how much to allocate. ...
    (comp.lang.c)