[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: passing array from c++ wrapper to c function
From: |
Andy Buckle |
Subject: |
Re: passing array from c++ wrapper to c function |
Date: |
Tue, 1 Mar 2011 22:05:32 +0000 |
> Now, I am stuck again, because I cannot pass back to C++ a vector from C.
In C you have to worry about allocating and freeing memory. If you
created (allocated memory for) an array within your C function, then
you would have to worry about freeing it. If you use the Octave API to
create your matrix to return, then I think* that Octave takes care of
this for you.
The C way is to be given pointers to pre-allocated arrays, one with
the input, and one for the output. This is how I would do it
*
and would appreciate correction from those more knowledgeable
---------------------------
#include <octave/oct.h>
extern "C"
{
void t3 (double *p_out, double *p_in, int len)
{
for (int i=0; i<len; i++)
{
p_out[i]=p_in[i]*2;
}
}
}
DEFUN_DLD (t3, args, , "test interpolation")
{
octave_value_list retval;
Array<double> ar = args(0).array_value ();
int len=ar.length();
// create output arg
Matrix out(ar.rows(), ar.columns());
t3(out.fortran_vec(), ar.fortran_vec(), len);
retval(0) = octave_value(out);
return retval ;
}
---------------------------
--
/* andy buckle */