help-octave
[Top][All Lists]
Advanced

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

Unexpected OCTAVE_LOCAL_BUFFER behavior


From: mpender
Subject: Unexpected OCTAVE_LOCAL_BUFFER behavior
Date: Mon, 6 Jan 2014 20:16:56 -0800 (PST)

I'm trying to use the C++ function call OCTAVE_LOCAL_BUFFER for the first
time and it is not behaving as I would expect.  However, this may or may not
be a bug, and I don't want to submit a bug report since this may simply be
my inexperience with the way the function actually works or an error in my
pointer syntax.

I wrote a short Octave script that calls an oct file which reserves memory
by two different methods.  The first uses a Matrix constructor and the
second uses the OCTAVE_LOCAL_BUFFER call.  Then I assign the buffer to a
global variable so I can keep the buffer accessible after returning from the
oct file.  The Matrix constructor appears to work correctly, but the
OCTAVE_LOCAL_BUFFER call always returns a scalar with the value 1.  The
Octave script, oct file code and run results are as follows:

Octave file: test15.m
clear all
global P1
global P2
resmem (100, 10);

Oct file: resmem.cc
#include <octave/oct.h>

DEFUN_DLD (resmem, args, nargout,  "Reserve Memory for P1 and P2 with two
parameters, parm1 and parm2.")
{
  octave_value retval;
  int nargin = args.length();

  if (nargin != 2)
     print_usage ();
  else
  {
    const int parm1 = args(0).int_value();
    const int parm2 = args(1).int_value();

    octave_stdout << " Parm1 " << parm1 << "\n Parm2 " <<
      parm2 << "\n";

// Using the operation:  Matrix tmp1 (parm1, parm2);
// consumes more memory than necessary since Matrix uses doubles.
// An area for future improvement would be to reduce this to single
// precision floating point or ints. 

// Allocate memory
    Matrix tmp1 (parm1, parm2);

// It isn't really necessary to zero the memory before starting, but 
// it is consistent with the original Octave code.
    for (int indx = 0; indx < parm1; indx++)
        for (int jndx = 0; jndx < parm2; jndx++)
            tmp1(indx, jndx) = 0.0;     

// Assign the memory space to the global pointer
    set_global_value ("P1", tmp1);

    OCTAVE_LOCAL_BUFFER (double, tmp2, parm1 * parm2);
    for (int indx = 0; indx < parm1; indx++)
        for (int jndx = 0; jndx < parm2; jndx++)
            tmp2[indx, jndx] = 0.0;
    set_global_value ("P2", tmp2);

    return octave_value_list();
   }
}

Test results:
octave:29> mkoctfile resmem.cc
octave:30> test15
 Parm1 100
 Parm2 10
octave:31> sizeof(P1)
ans =  8000
octave:32> sizeof(P2)
ans =  1
octave:33> P2
P2 =  1
octave:34> 





--
View this message in context: 
http://octave.1599824.n4.nabble.com/Unexpected-OCTAVE-LOCAL-BUFFER-behavior-tp4660725.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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