9

I'm trying to Use Octave with Visual C++.

I have downloaded octave-3.6.1-vs2010-setup-1.exe. Created a new project, added octave include folder to include path, octinterp.lib and octave.lib to lib path, and I added Octave bin folder as running directory.

The program compiles and runs fine except feval function that causes the exception:

Microsoft C++ exception: octave_execution_exception at memory location 0x0012faef

and on Octave side:

Invalid resizing operation or ambiguous assignment to an out-of-bounds array element.

What am I doing wrong?


Code for a standalone program:

#include <octave/octave.h>
#include <octave/oct.h>
#include <octave/parse.h>

int main(int argc, char **argv)
{
    if (octave_main (argc, argv, true))
    {
        ColumnVector NumRands(2);
        NumRands(0) = 10;
        NumRands(1) = 1;
        octave_value_list f_arg, f_ret;
        f_arg(0) = octave_value(NumRands);
        f_ret = feval("rand",f_arg,1);
        Matrix unis(f_ret(0).matrix_value());
    }
    else
    {
        error ("Octave interpreter initialization failed");
    }
    return 0;
}

Thanks in advance.

Amro
  • 123,847
  • 25
  • 243
  • 454
user629926
  • 1,910
  • 2
  • 18
  • 43

2 Answers2

7

I tried it myself, and the problem seems to originate from the feval line.

Now I don't have an explanation as to why, but the problem was solved by simply switching to the "Release" configuration instead of the "Debug" configuration.

I am using the Octave3.6.1_vs2010 build, with VS2010 on WinXP.

Here is the code I tested:

#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>

int main(int argc, char **argv)
{
    // Init Octave interpreter
    if (!octave_main(argc, argv, true)) {
        error("Octave interpreter initialization failed");
    }

    // x = rand(10,1)
    ColumnVector sz(2);
    sz(0) = 10; sz(1) = 1;
    octave_value_list in = octave_value(sz);
    octave_value_list out = feval("rand", in, 1);

    // print random numbers
    if (!error_state && out.length () > 0) {
        Matrix x( out(0).matrix_value() );
        std::cout << "x = \n" << x << std::endl;
    }

    return 0;
}

with an output:

x =
 0.165897
 0.0239711
 0.957456
 0.830028
 0.859441
 0.513797
 0.870601
 0.0643697
 0.0605021
 0.153486
Amro
  • 123,847
  • 25
  • 243
  • 454
1

I'd guess that its actually stopped pointing at the next line and the error actually lies at this line:

f_arg(0) = octave_value(NumRands);

You seem to be attempting to get a value (which value?) from a vector and then assigning it to element 0 of a vector that has not been defined as a vector.

I don't really know though ... I've never tried writing octave code like that. I'm just trying to work it out by translating the code to standard matlab/octave code and that line seems really odd to me ...

Goz
  • 61,365
  • 24
  • 124
  • 204
  • 1
    I checked that it's not it. Also I found that eval_string always throws std::bad_aloc exception. Just in case I tried to run second sample from http://www.gnu.org/software/octave/doc/interpreter/Standalone-Programs.html and it throws at 'argv(0) = "embedded" ', so I tried 'const std::string a="embedded"; argv.append(a)' and it also throws and the debuger show a sometimes as some junk sometimes as BadPtr which is all very strange. I'm new to c++ ,maybe I'm missing something obvious. – user629926 Jun 22 '12 at 04:00