help-octave
[Top][All Lists]
Advanced

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

what's wrong with this code?


From: John W. Eaton
Subject: what's wrong with this code?
Date: Sat, 20 Feb 1999 02:15:45 -0600 (CST)

On 20-Feb-1999, address@hidden <address@hidden> wrote:

| I'm not a C++ wizard but I write .oct files from time to time.  Usually it
| works fine but today I wrote a new one and it causes octave to dump core.
| I kept simplifying it to figure out where the problem is but no luck.
| Now it's down to just a few lines and stil has the same problem but I
| don't see it.  The code is below.  Put this in a file called showit.cc.
| Compile it with mkoctfile and it compiles with no errors.  Now do the
| following
| 
| octave:1> dw = randn(9000,1);
| octave:2> [a b] = showit(.08,.08,dw); 
| 
| and it dumps core.  Any ideas?

I think you are indexing Target, r, and dw outside their bounds.

Probably you want to dimension them something like this:

  int NumberElements = dw.length()-1;
  ColumnVector r(NumberElements+1,0);
  ColumnVector Target(NumberElements+1,0);

because your loop

   for (int i=0; i < NumberElements; i++)
     {
       Target(i+1) = Target(i);  
       r(i+1)=r(i) + dw(i+1);
     }

accesses Target and r from 0 through NumberElements and dw from 1
through NumberElements.

When you write outside the bounds of the array data, you corrupt some
important information in the ColumnVector class data, then when Octave
tries to create the return values it crashes in malloc when it
attempts to put the ColumnVector data in a Matrix object.

jwe



reply via email to

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