c++ - Avoiding Segmentation Fault in .oct function? -


this directed @ users of octave.

in c++ api documentation octave, 1 of examples function adds 2 matrices so:

*/addtwomatrices.cc #include <octave/oct.h> defun_dld (addtwomatrices, args, , "add b") {   if (args.length () != 2)     print_usage ();    ndarray = args(0).array_value ();   ndarray b = args(1).array_value ();   return octave_value (a + b); } 

link: https://www.gnu.org/software/octave/doc/interpreter/matrices-and-arrays-in-oct_002dfiles.html#matrices-and-arrays-in-oct_002dfiles

which works fine , compiles without errors. when using in octave though, if function called incorrect number of arguments, seg faults , exits program , dumps octave-core.

is there way keep octave open if function called incorrect number of arguments? example,

addtwomatrices(5) octave 1> "incorrect number of arguments supplied. please provide 2." octave 2>' 

instead of

invalid call addtwomatrices.  correct usage is:  adds 2 matrices additional built-in functions , operators available in on-line version of manual.  use command `doc <topic>' search manual index.  , information octave available on www @ http://www.octave.org , via help@octave.org mailing list. panic: segmentation fault -- stopping myself... attempting save variables `octave-core'... save `octave-core' complete segmentation fault 

you reading online manual last release of octave (4.2.1), not using octave 4.2.1. need check documentation octave version:

if using octave 4.2.x release, behave expected:

$ cat addtwomatrices.cc  #include <octave/oct.h> defun_dld (addtwomatrices, args, , "add b") {   if (args.length () != 2)     print_usage ();    ndarray = args(0).array_value ();   ndarray b = args(1).array_value ();   return octave_value (a + b); } $ mkoctfile-4.2.1 addtwomatrices.cc  $ octave-cli-4.2.1 -q octave-cli-4.2.1:1> addtwomatrices (5) error: invalid call addtwomatrices.  correct usage is:  add b octave-cli-4.2.1:1> $ mkoctfile-4.0.0 addtwomatrices.cc  $ octave-cli-4.0.0 -q octave-cli-4.0.0:1> addtwomatrices (5) error: invalid call addtwomatrices.  correct usage is:  add b panic: segmentation fault -- stopping myself... segmentation fault 

the way errors handling in octave's libinterp changed in 4.2. if using older version, need handle return of function yourself. should instead this:

#include <octave/oct.h> defun_dld (addtwomatrices, args, , "add b") {   if (args.length () != 2)     {       print_usage ();       // in octave 4.2, print_usage throws exceptions       // , exist function.  same behaviour       // in octave m language.  in octave 4.0, prints       // usage , still need return otherwise       // continues execution of function.       return octave_value ();      }    ndarray = args(0).array_value ();   ndarray b = args(1).array_value ();   return octave_value (a + b); } 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -