help-octave
[Top][All Lists]
Advanced

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

Persistent dara across .oct-files


From: Felix Wolf
Subject: Persistent dara across .oct-files
Date: Fri, 10 Aug 2018 10:54:55 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1

Hello everyone,

my colleague and I are working on a larger academic C++ code, which we
want to equip with octave wrappers such that it can be used by students.
For multiple reasons, we cannot port certain data structures to octave,
and we cannot pass certain data as copy between functions.

We found out, that under Ubuntu (bionic, Octave 4.2.2), this behavior
can be achieved via global variables in .oct-files, as can be seen from
the following minimal example:

init.cpp:

- - - - -

#include <octave/oct.h>

double my_const;

DEFUN_DLD(init, args, nargout, "init") {
    octave_stdout << &my_const << "\n";
    my_const = 42;
    octave_stdout << my_const << "\n";
  return octave_value_list();
}

extract.cpp:

- - - - -

#include <octave/oct.h>

extern double my_const;

DEFUN_DLD(extract, args, nargout, "extract") {
    octave_stdout << &my_const << "\n";
    octave_stdout << my_const << "\n";
    return octave_value(1);
}

- - - - -

These functions compile and run by calling

mkoctfile -std=c++11 init.cpp
mkoctfile -std=c++11 extract.cpp
init()
extract()

and, when called, return the same value and the same adress, for example

0x7f5b429c40c8
42
0x7f5b429c40c8
42

However, these files compile neither under Mac nor Windows. On Windows,
one cannot compile and run them as mentioned above, but by calling an
.m-file of the form

mkoctfile init.cpp
mkoctfile extract.cpp init.cpp
init()
extract()

but they do not yield the desired output. This brings me to the two
underlying questions we have.

1. Is the behavior of the example above under Ubuntu intended or a bug,
i.e., can we depend on it to be reproducable in upcoming versions?
2. Is there an elegant solution to make certain data structures persist
in memory across multiple .oct-files (for example, by passing a
reference/pointer to octave)?

Thank you for your time and assistance,

Felix Wolf





reply via email to

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