bspline 基底関数を評価するためのクラスを定義しました。ポインターや新しい削除などを使用したことはありません。クラスは次のとおりです。
class bspline_basis{
//private:
public:
int k; /*! order of Bspline basis */
int nbreak; /*! Dimension of breakpoints vector. */
int nknots; /*! Dimension of knots vector. */
int nbasis; /*! Number of basis functions */
vector<double> breakpts; /*! Represents strictly increasing values of knots, excluding all multiplicities */
vector<double> knots; /*! Raw knot vector of BSpline basis, it may contain multiple entries due to multiplicity */
vector<double> Bix_nonzero; /*! size: (k). Stores nonzero components of bspline basis */
vector<double> Bix; /*! size: nbasis Stores all components of bspline basis. Not necessary - remove? */
int find_knot_span_of_x(const double &x); /*! Returns integer i: t_i <= x < t_{i+k}. Upon call it stores i in i_saved */
pair<int,int> find_nonzero_basis_at_x(const double &x); /*! Returns first, last index of nonzero basis B_i(x) at particular x. */
pair<int,int> find_base_nonzero_interval(const double &x); /*! Returns first (i) , last (i+k) index of knots t_i at particular x. */
int i_saved; // Temporary saves for speed up
double x_saved; // Temporary saves for speed up
/* !ESSENTIAL ROUTINES FOR EVALUATION! Add as optional argument another knot vector for use in evaluation of integrals */
void eval_nonzero_basis(const int &i, const double &x); /*! Evaluates non zero basis functions at x */
void eval_Bix(const int &i, const double &x); /*! Evaluates all basis functions at x */
/*! Default clamped knot vector constructor */
bspline_basis(const vector<double> &_breakpts, const int &_k);
/* Evaluation functions */
double get_Bix(const int &i, const double &x); /*! Value B_i(x) */
};
ユーザーに対して透過的で、関数 B_i(x) を評価する関数は次のとおりです。
get_Bix(const int &i, const double &x);
可変整数 i を使用して for ループ内で使用すると、すべてがうまく機能します。つまり、この例では次のようになります。
// some constructor of class mybasis
for(double x=0; x<=10. x+=0.01)
{
cout<< x << " ";
for (int i=0; i<nbasis; ++i)
cout<< mybasis.get_Bix(i,x)<<" ";
cout<<endl;
}
正しい値が出力されます。ただし、次の例のように、関数の最初の引数に定数整数を定義すると、次のようになります。
int idx=3;
for(double x=0; x<=10. x+=0.01)
{
cout<< x << " ";
//for (int i=0; i<nbasis; ++i)
cout<< mybasis.get_Bix(idx,x)<<" ";
cout<<endl;
}
次のエラーが表示されます。
*** Error in `./test_class.xxx': double free or corruption (out): 0x0000000000740280 ***
gdb でコードを実行してバックトレースすると、次のメッセージが表示されます。
(gdb) bt
#0 0x00007ffff7530bb9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linu/raise.c:56
#1 0x00007ffff7533fc8 in __GI_abort () at abort.c:89
#2 0x00007ffff756de14 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff767c668 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3 0x00007ffff757a0ee in malloc_printerr (ptr=<optimised out>, str=0x7ffff767c798 "double free or corruption (out)", action=1) at malloc.c:4996
#4 _int_free (av=<optimised out>, p=<optimised out>, have_lock=0) at malloc.c:3840
#5 0x00000000004039ac in __gnu_cxx::new_allocator<double>::deallocate (this=0x7fffffffdc70, __p=0x608280) at /usr/include/c++/4.8/ext/new_allocator.h:110
#6 0x00000000004031be in std::_Vector_base<double, std::allocator<double> >::_M_deallocate (this=0x7fffffffdc70, __p=0x608280, __n=15) at /usr/include/c++/4.8/bits/stl_vector.h:174
#7 0x00000000004030b3 in std::_Vector_base<double, std::allocator<double> >::~_Vector_base (this=0x7fffffffdc70, __in_chrg=<optimised out>) at /usr/include/c++/4.8/bits/stl_vector.h:160
#8 0x00000000004028ed in std::vector<double, std::allocator<double> >::~vector (this=0x7fffffffdc70, __in_chrg=<optimised out>) at /usr/include/c++/4.8/bits/stl_vector.h:416
#9 0x00000000004017aa in bspline_basis::eval_Bix (this=0x7fffffffdd40, ii=4, x=@0x7fffffffdd10: 0.01) at bsplines_stackoverflow.hpp:247
#10 0x0000000000401f59 in bspline_basis::get_Bix (this=0x7fffffffdd40, i=4, x=@0x7fffffffdd10: 0.01) at bsplines_stackoverflow.hpp:331
#11 0x000000000040214e in main () at test_class.cpp:31
問題は機能にあるに違いない
double bspline_basis::get_Bix(const int &i, const double &x)
{
if (i<0 || i> nbasis){
DEBUG(i);
std::cerr<< "Index of Bix out of range, aborting ..." << endl;
throw 0;
}
if (x==x_saved && i==i_saved) {
return
Bix[i];
}else if ( x != x_saved && i == i_saved){
eval_Bix(i_saved,x); // Evaluate all nonzero and store to Bix.
x_saved=x; // Store x for subsequent evaluations.
return
Bix[i];
}else {
// a. Find knot span of x:
find_knot_span_of_x(x);
// b. Evaluate all nonzero Bix and pass them to Bix:
eval_Bix(i_saved,x);
x_saved=x; // Store x for subsequent evaluations.
i_saved=i; // Store knot span i for possible subsequent evaluations.
return
Bix[i];
}
}
と
/*!
Wrapper function for eval_nonzero_basis. Passes nonzero basis values to vector<double> Bix.
*/
void bspline_basis::eval_Bix (const int &ii, const double &x){
//pair<int,int> i_start_end = find_nonzero_basis_at_x(x);
int istart= ii-k+1;
pair<int,int> i_start_end = make_pair(istart,ii);
// Evaluate all nonzero entries. for this index.
eval_nonzero_basis(i_start_end.second, x);
// Initialize (to zeros) temporary vector of dimension nbasis
vector<double> Bix_temp(nbasis,0.0);
// Pass nonzero entries to temporary vector
for(int j= i_start_end.first; j <= i_start_end.second; ++j)
Bix_temp[j] = Bix_nonzero[j-i_start_end.first];
// move temporary vector to Bix
Bix=Bix_temp;
}
ループの外で最初の引数を定義したときにエラーが発生する可能性があることを理解できません。どんな助けでも大歓迎です。
更新:インデックス (idx=3) が許容範囲外にあるためではないことを明確にしてください。この場合、最初の for ループがクラッシュします。変数 nbasis が 3 よりはるかに大きい。
更新 2: @Adrian の提案に従って、Bix_temp をクラスの要素にし、コードを再実行しました。再びコードがクラッシュしますが、何らかの出力 (x と infs の値) が生成されることがわかります。これは、デバッガーの新しい出力です。
9.97 inf
9.98 inf
9.99 inf
10 inf
0.002867sec
4.78333e-05min
*** Error in `/home/foivos/Documents/Grav_Ast/BSplines/Genetic_Algorithms/tests/test_new_Jeans/bsplines_class/test_class.xxx': double free or corruption (out): 0x0000000000608180 ***
Program received signal SIGABRT, Aborted.
0x00007ffff7530bb9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7530bb9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1 0x00007ffff7533fc8 in __GI_abort () at abort.c:89
#2 0x00007ffff756de14 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff767c668 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3 0x00007ffff757a0ee in malloc_printerr (ptr=<optimised out>, str=0x7ffff767c798 "double free or corruption (out)", action=1) at malloc.c:4996
#4 _int_free (av=<optimised out>, p=<optimised out>, have_lock=0) at malloc.c:3840
#5 0x00000000004039e8 in __gnu_cxx::new_allocator<double>::deallocate (this=0x7fffffffdda8, __p=0x608180) at /usr/include/c++/4.8/ext/new_allocator.h:110
#6 0x00000000004031fa in std::_Vector_base<double, std::allocator<double> >::_M_deallocate (this=0x7fffffffdda8, __p=0x608180, __n=15) at /usr/include/c++/4.8/bits/stl_vector.h:174
#7 0x00000000004030ef in std::_Vector_base<double, std::allocator<double> >::~_Vector_base (this=0x7fffffffdda8, __in_chrg=<optimised out>) at /usr/include/c++/4.8/bits/stl_vector.h:160
#8 0x0000000000402929 in std::vector<double, std::allocator<double> >::~vector (this=0x7fffffffdda8, __in_chrg=<optimised out>) at /usr/include/c++/4.8/bits/stl_vector.h:416
#9 0x0000000000402646 in bspline_basis::~bspline_basis (this=0x7fffffffdd30, __in_chrg=<optimised out>) at bsplines_stackoverflow.hpp:57
#10 0x00000000004022b6 in main () at test_class.cpp:22
(gdb) frame 9
#9 0x0000000000402646 in bspline_basis::~bspline_basis (this=0x7fffffffdd30, __in_chrg=<optimised out>) at bsplines_stackoverflow.hpp:57
57 class bspline_basis{
(gdb) info args
this = 0x7fffffffdd30
__in_chrg = <optimised out>
(gdb)
繰り返しますが、for ループ インデックス i を使用すると、エラーは発生しません。
更新 3:バグを発見しました。これは概念的なものであり、関数内にありました
get_Bix(const int &i, const double &x)
具体的には、次のコード行で:
// Was using the same index i, for different x, which was a mistake and was causing troubles.
}else if ( x != x_saved && i == i_saved){
eval_Bix(i_saved,x); // Evaluate all nonzero and store to Bix.
x_saved=x; // Store x for subsequent evaluations.
return
Bix[i];
}else {
ご協力ありがとうございました。