Actually, I think she was suggesting using pack.  I have done something similar -- I 
used something like:

@arr = (1,2,3);
$packedarray=pack("c3",@arr);
$sum = inline_sum($packedarray, scalar(@arr))

and in the inline part (here is a simple example which adds the elements in the array)

int inline_sum(char *array, int length) {
   /* treat *array as a C-array of bytes */
   int sum = 0;
   int i;
   for (i = 0; i < length; ++i,++array) {
      sum += *array;
   } 
   return sum;
}


In this case I knew the values would fit in a byte.  If not, use "i" as the pack code, 
and cast the pointer to be a pointer to int, or "f" for float, etc.

Speed-wise I would expect this to be similar to Neil's technique, but this would be 
cleaner to read since the "pack" is doing all the work, and you don't need to worry as 
much about memory management issues.  You just have to learn "pack" and understand 
casting of pointers in C.

Best of luck,
-Jonathan


--- Begin Message ---
Hi Liz,

So you're saying to do something like:

my $str = join(@arr, / /); # to create a string of each element separated 
                            # by spaces
&cFunction(\$str);  # then pass the string reference to the c function

__DATA__
__C__

void cFunction(SV* str) {

// Convert the string ref into a C array

}

That sounds like a great idea if you know a
more efficient way of creating an array from this string
reference than Neil showed with his for loop.
I'm very new to C, can you show me how to do this?

In order to interface with Matlab's Math and Graphics libraries, I
have to create a mxArray using the following prototype:

mxArray *mlfDoubleMatrix(int m, int n, const double *pr, 
                                       const double *pi);
Where:
int m = Number of rows.
int n = Number of columns.
const double *pr = Pointer to values to initialize the mxArray array
                   vector of real values.
const double *pi = Pointer to values to initialize the mxArray array
                   vector of imaginary values. Specify NULL if there 
                   is no imaginary part.

This is the only matlab function that I have found which doesn't 
take a pre-existing mxArray.

Thanks...Brady


-----Original Message-----
From: Elizabeth Mattijsen [mailto:[EMAIL PROTECTED]]
Sent: Monday, June 10, 2002 2:23 PM
To: bbcannon; 'Neil Watkiss'
Cc: '[EMAIL PROTECTED]'
Subject: RE: Instantiating arrays with Inline


At 01:56 PM 6/10/02 -0600, bbcannon wrote:
>I only have one gripe(sorry!).  The arrays that I will be working with
>could potentially have thousands of entries.  Having to iterate
>through each element of each array seems like an extremely large waste
>of time.
>I obviously don't understand what it takes to convert a perl
>reference to a C reference (if it can be done at all), but is
>it not possible? Or is that just a "Perfect World" scenario?

Couldn't you -pack- your array into a scalar and pass a reference to that?


Liz


--- End Message ---

Reply via email to