增强功能的奇怪行为
weird behavior of boost function
如果我注释掉构造函数中的第一行,我的以下代码就会出错。 return 错误是:
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::overflow_error> >: Error in function boost::math::cyl_bessel_k<double>(double,double): numeric overflow
Abort trap: 6
然而,奇怪的是,如果我在构造函数中输出一些东西(例如,取消注释第一行),那么我的程序运行正常。
GP::GP(const Training_set& _sample, Eigen::VectorXd& param,
const std::string& which_kernel) : sample(_sample)
{
// std::cout << "WORKS" << std::endl;
if (which_kernel == "Matern") {
std::cout << "Matern kernel is used!" << std::endl;
Kernel_Matern matern(param, param.size());
kernel = &matern;
} else if (which_kernel == "Square_Exponential") {
std::cout << "Square Exponential kernel is used!" << std::endl;
Kernel_SE se(param, param.size());
kernel = &se;
} else {
std::cout << "Cannot identify the kernel" << std::endl;
exit(1);
}
input_dim = sample.dim;
input_size = sample.size;
L_chol.resize(sample.size, sample.size);
Eigen::MatrixXd cov_matrix = Eigen::MatrixXd::Zero(sample.size, sample.size);
get_cov_matrix(sample.X, input_size, sample.X, input_size, cov_matrix);
get_chol(cov_matrix);
}
您正在存储超出范围的临时地址。在指向的内容超出范围后使用 *kernel
是未定义的行为。
kernel
应该是类型 std::unique_ptr<X>
而不是类型 X*
.
将作业替换为:
kernel = std::make_unique<Kernel_Matern>(param, param.size());
或:
kernel = std::make_unique<Kernel_SE>(param, param.size());
在有问题的两行。
如果您有将 kernel
传递给函数的代码,请改为传递 kernel.get()
。
请注意,它会阻止复制 GP
的实例但不会移动它们,因为 unique ptr 是只能移动的。如果你有一个类型将值和指针都存储到它自己的值中,无论如何复制它可能是一个错误。
如果我注释掉构造函数中的第一行,我的以下代码就会出错。 return 错误是:
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::overflow_error> >: Error in function boost::math::cyl_bessel_k<double>(double,double): numeric overflow
Abort trap: 6
然而,奇怪的是,如果我在构造函数中输出一些东西(例如,取消注释第一行),那么我的程序运行正常。
GP::GP(const Training_set& _sample, Eigen::VectorXd& param,
const std::string& which_kernel) : sample(_sample)
{
// std::cout << "WORKS" << std::endl;
if (which_kernel == "Matern") {
std::cout << "Matern kernel is used!" << std::endl;
Kernel_Matern matern(param, param.size());
kernel = &matern;
} else if (which_kernel == "Square_Exponential") {
std::cout << "Square Exponential kernel is used!" << std::endl;
Kernel_SE se(param, param.size());
kernel = &se;
} else {
std::cout << "Cannot identify the kernel" << std::endl;
exit(1);
}
input_dim = sample.dim;
input_size = sample.size;
L_chol.resize(sample.size, sample.size);
Eigen::MatrixXd cov_matrix = Eigen::MatrixXd::Zero(sample.size, sample.size);
get_cov_matrix(sample.X, input_size, sample.X, input_size, cov_matrix);
get_chol(cov_matrix);
}
您正在存储超出范围的临时地址。在指向的内容超出范围后使用 *kernel
是未定义的行为。
kernel
应该是类型 std::unique_ptr<X>
而不是类型 X*
.
将作业替换为:
kernel = std::make_unique<Kernel_Matern>(param, param.size());
或:
kernel = std::make_unique<Kernel_SE>(param, param.size());
在有问题的两行。
如果您有将 kernel
传递给函数的代码,请改为传递 kernel.get()
。
请注意,它会阻止复制 GP
的实例但不会移动它们,因为 unique ptr 是只能移动的。如果你有一个类型将值和指针都存储到它自己的值中,无论如何复制它可能是一个错误。