help-octave
[Top][All Lists]
Advanced

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

Re: fortran_vec() vs data() - very first element is lost


From: Patrick Boettcher
Subject: Re: fortran_vec() vs data() - very first element is lost
Date: Tue, 25 Oct 2016 00:02:15 +0200

On Mon, 24 Oct 2016 08:59:30 -0700
Mike Miller <address@hidden> wrote:

> On Mon, Oct 24, 2016 at 16:07:18 +0200, Patrick Boettcher wrote:
> > Hi list,
> > 
> > I'm still working in an .oct-file to get in and out complex and
> > double matrices.
> > 
> > I noticed something strange which makes me wonder whether I really
> > got it correctly:
> > 
> > I read here 
> > 
> >   
> > https://www.gnu.org/software/octave/doc/v4.0.1/Matrices-and-Arrays-in-Oct_002dFiles.html
> > 
> > that I should use the method fortran_vec() to get to the raw-buffer.
> > 
> > It seems I'm using it correctly to fill data into a matrix returned
> > by my oct-function. However, when using this method to access the
> > data of a matrix given to my function as an argument the very first
> > element contains garbage:  
> 
> You may have found a bug with this example. I get the same bad result
> as you, but other small variations on this theme give the correct
> values.
> 
> Try
> 
>   auto matrix = args(0).complex_matrix_value ();
>   auto fdata = matrix.fortran_vec ();
>   auto data = matrix.data ();

If this works (and it does), I have a possible explanation:

What does args(0).complex_matrix_value().data() does?

It creates a temporary matrix, args(0).complex_matrix_value(), of which
I request a pointer to its internal data-structure.

Just when I stored the pointer in my fdata-variable the matrix is no
longer needed and it destructed. 

Matrix (and its base-classes) do not know that I still keep a pointer,
so I assume it is garbage-collected == deleted.

Typically with freed memory the first bytes/element are overwritten or
immediately reused in subsequent calls. 

Mike, you're doing it correctly. I need to keep the matrix-alive. Maybe
a reference will work, this would avoid the copy of the internal
structure.

Thanks for your help (Olaf and c. as well),
-- 
Patrick.



reply via email to

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