help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Include Fortran Code which Returns Arrays of Dynamic/Unknown Size


From: John W. Eaton
Subject: Re: Include Fortran Code which Returns Arrays of Dynamic/Unknown Size
Date: Wed, 08 Oct 2008 17:28:45 -0400

On  8-Oct-2008, Marco2008 wrote:

| My problem is the following fortran code:
| 
| subroutine funfortrantwo(vector1)
|       implicit none
|       integer :: size1
|       real(8), allocatable :: vector1(:)
|       size1 = 3       
|       ! size1 here is defined by a fixed number only to make the 
|       ! example clear. Normally, it is computed inside this fortran
|       ! subroutine and not known before a call of funfortrantwo.
|       allocate( vector1(size1) )
|       vector1 = 2
| end subroutine funfortrantwo
| 
| I do not know how to write the c++-wrapper for this subroutine. In the first
| example the size of the matrix is known before the fortran subroutine is
| called. But this is not the case in the second example.

I don't know how to call this function from C/C++.

If you had a similar function in C++, for example something like

  void foo (double **v, int *len)
  {
    len = 10; // or some computed value
    *v = new double [len];

    // fill V here ...
  }

then you could call it from an Octave DEFUN like this:

  double *v;
  int len;
  foo (&v, &len);

and then copy the values from V to an appropriately sized Matrix
object:

  Matrix m (len, 1);
  double *pm = m.fortran_vec ();
  for (int i = 0; i < len; i++)
    m[i] = v[i];

then you would probably also want to free the memory allocated in FOO
to avoid a memory leak:

  delete [] v;

Is an allocatable array in Fortran some kind of object that carries
with it the size?  If so, then I think you need to know precisely what
your compiler does so that you can call a function like this from C++
(probably the details are implementation defined, so will not be
portable).  Or is it guaranteed to just be a pointer to some data?  If
it is just a pointer, then I think you will need to modify your
function so that it also returns the size.  Otherwise, how can a C++
function that calls your Fortran function know how large the allocated
array is?

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]