MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problems using MathLink

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95313] Re: Problems using MathLink
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Thu, 15 Jan 2009 06:11:03 -0500 (EST)
  • References: <gkgq0p$48v$1@smc.vnet.net> <200901141031.FAA17019@smc.vnet.net> <A6D29BB9-24ED-4F10-90C5-F86B4A92DFD3@wolfram.com>

Hi,

you also not free the memory !

void rot(MLINK link, double theta)
{

/*
Because the original function was only sending Heads of type List,
We can simply pass MLPutRealArray a NULL pointer for the heads
argument and MathLink will send the head List for each level of the array.
*/

double a[3][3];
a[0][0] = cos(theta);    a[0][1] = sin(theta);   a[0][2] = 0;
a[1][0] = - sin(theta);  a[1][1] = cos(theta);  a[1][2] = 0;
a[2][0] = 0;                 a[2][1] = 0;               a[2][2] = 1;

long *dims, depth;

/*
The array is only depth 2.  The original version told MLPutRealArray that
the data array 'a' had a depth of 3.   MLPutRealArray tried to send the 
extra dimension
which didn't actually exist which lead to the crash.
*/

dims = new long[2];
dims[0] = 3;  dims[1] = 3;
MLPutRealArray(link, *a, dims, (char **)0, 2);

/*************************************************
 because otherwise, every call waste 32 byte of heap memory
   ************************************************/
delete[] dims;
}

Regards
  Jens

Steve Wilson wrote:
> Here are the modifications that this function needs in order to work 
> correctly:
>
> void rot(MLINK link, double theta)
> {
>
> /*
> Because the original function was only sending Heads of type List,
> We can simply pass MLPutRealArray a NULL pointer for the heads
> argument and MathLink will send the head List for each level of the 
> array.
> */
>
> double a[3][3];
> a[0][0] = cos(theta);
> a[0][1] = sin(theta);
> a[0][2] = 0;
> a[1][0] = - sin(theta);
> a[1][1] = cos(theta);
> a[1][2] = 0;
> a[2][0] = 0;
> a[2][1] = 0;
> a[2][2] = 1;
>
> long *dims, depth;
>
> /*
> The array is only depth 2.  The original version told MLPutRealArray that
> the data array 'a' had a depth of 3.   MLPutRealArray tried to send 
> the extra dimension
> which didn't actually exist which lead to the crash.
> */
>
> dims = new long[2];
> dims[0] = 3;
> dims[1] = 3;
> MLPutRealArray(link, *a, dims, (char **)0, 2);
> }
>
>
> Steve Wilson
> Senior Network/System Protocol Developer
> Wolfram Research, Inc.
>
>
> On Jan 14, 2009, at 3:31 AM, Jens-Peer Kuska wrote:
>
>> Hi,
>>
>> you are sure that your array has the depth 3 and not
>> the dimension 3 x 3, and what is the third dimension
>> in a[3][3] ..
>>
>> BTW, you never delete[] heads,dims.
>> If you wish to fill your main memory you should
>> use larger pieces of memory
>> that you don't recycle, you are faster done than.
>>
>> Regards
>>   Jens
>>
>> Jonathan Yik wrote:
>>> Hi,
>>>
>>> I'm trying to write a subroutine in C++ that passes a rotation 
>>> matrix to
>>> Mathematica through MathLink.  The code compiles fine, but when I 
>>> try to run
>>> the compiled program, I get a segfault at the point in the program 
>>> where the
>>> subroutine is called.  As far as I can tell, the fault occurs 
>>> whenever the
>>> program calls MLPutRealArray or MLPutReal32Array.
>>>
>>> Here is my code for the subroutine:
>>>
>>> void rot(MLINK link, double theta)
>>> {
>>>  char **heads, **HEAD;
>>>  heads = new char*[3];
>>>  heads[0] = "List";
>>>  heads[1] = "List";
>>>  heads[2] = "List";
>>>  double a[3][3], *b;
>>>  a[0][0] = cos(theta);
>>>  a[0][1] = sin(theta);
>>>  a[0][2] = 0;
>>>  a[1][0] = - sin(theta);
>>>  a[1][1] = cos(theta);
>>>  a[1][2] = 0;
>>>  a[2][0] = 0;
>>>  a[2][1] = 0;
>>>  a[2][2] = 1;
>>>  long *dims, *D, depth;
>>>  dims = new long[3];
>>>  dims[0] = 3;
>>>  dims[1] = 3;
>>>  dims[2] = 3;
>>>  MLPutRealArray(link, *a, dims, heads, 3);
>>> }
>>>
>>> Is there something in this code that I'm missing that causes the 
>>> fault?  Or
>>> is this a but in the ml32i3.dll file?
>>>
>>> Yours,
>>>
>>> Johnathan Yik
>
>



  • Prev by Date: Re: Problems using MathLink
  • Next by Date: Re: Taking LCM in a polynomial expression
  • Previous by thread: webMathematica - comments, opinions, user group (?)
  • Next by thread: Re: Solving nonlinear coupled differential equations in Mathematica