[英]Segmentation fault
This is probably a silly question, but I can't see what I am doing wrong here. 这可能是一个愚蠢的问题,但是我看不到我在做什么错。 I have the class:
我上课:
#include <sys/time.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_randist.h>
#include <cmath>
#include "randomnumbergenerator.h"
class RandomNumberGenerator
{
gsl_rng * rn;
public:
RandomNumberGenerator();
~RandomNumberGenerator();
double univariate();
void bivariateGaussian(double rho, double &x, double &y);
};
long currentMicroseconds()
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_usec;
}
RandomNumberGenerator::RandomNumberGenerator()
{
const gsl_rng_type * T;
gsl_rng_env_setup();
//T = gsl_rng_default;
T = gsl_rng_mt19937;
rn = gsl_rng_alloc (T);
gsl_rng_set(rn,currentMicroseconds());
}
double RandomNumberGenerator::univariate()
{
return gsl_rng_uniform(rn);
}
void RandomNumberGenerator::bivariateGaussian(double rho, double &x, double &y)
{
gsl_ran_bivariate_gaussian (rn, 1.0, 1.0, rho, &x, &y);
}
RandomNumberGenerator::~RandomNumberGenerator()
{
gsl_rng_free (rn);
}
Which I call from here: 我从这里打电话:
double x;
double y;
rng.bivariateGaussian(rho, x, y);
but I get a segmentation fault on gsl_ran_bivariate_gaussian (rn, 1.0, 1.0, rho, &x, &y);
但是我在
gsl_ran_bivariate_gaussian (rn, 1.0, 1.0, rho, &x, &y);
上gsl_ran_bivariate_gaussian (rn, 1.0, 1.0, rho, &x, &y);
了分割错误
Any idea? 任何想法?
check to see if rn
has really been allocated. 检查是否真的分配了
rn
。 It is probably the only thing that can cause segmentation fault. 这可能是唯一会导致分段错误的原因。
i tested your code on my computer, it runs okay as far as they can tell. 我在计算机上测试了您的代码,据他们所知,它可以正常运行。 May be check installation of GSL, they have a test suite you can use
可能是检查GSL的安装,他们有一个可以使用的测试套件
Which compiler? 哪个编译器? I assume that
rn
is a member variable of RandomNumberGenerator. 我假设
rn
是RandomNumberGenerator的成员变量。 Do you initialize it to 0 in the constructor? 您是否在构造函数中将其初始化为0? You don't seem to be checking for an error return from gsl_rng_alloc, you probably should be because the only thing I can see right off that may be causing a problem is if
rn
isn't pointing to anything valid at the call that's segfaulting. 您似乎似乎没有在检查是否从gsl_rng_alloc返回错误,这可能是因为我可以立即看到的唯一可能导致问题的原因是,如果
rn
在指向段错误的调用时未指向任何有效内容。
Looking at the manual for gsl_rng_alloc
you can check to see if it returns NULL
or 0 and then throw an exception if it doesn't. 查看
gsl_rng_alloc
的手册,您可以检查它是否返回NULL
或0,然后返回异常。 For example: 例如:
#include <stdexcept>
RandomNumberGenerator::RandomNumberGenerator()
{
const gsl_rng_type * T;
gsl_rng_env_setup();
//T = gsl_rng_default;
T = gsl_rng_mt19937;
rn = gsl_rng_alloc (T);
if (rn == 0) {
throw ::std::runtime_error("Failed to allocation a random number generator.");
}
gsl_rng_set(rn,currentMicroseconds());
}
Also, have you tried compiling with -O0
to turn of all optimization? 另外,您是否尝试过使用
-O0
进行编译以进行所有优化?
In: 在:
double x;
double y;
rng.bivariateGaussian(rho, x, y);
are x and y perhaps supposed to be arrays rather than single variables? x和y可能应该是数组而不是单个变量? I'd expect a distribution to produce N values rather than one (or two).
我希望分布产生N个值,而不是一个(或两个)。
I don't program c++, but C. Hopefully this will someway also apply to you. 我不编程c ++,但我不编程C。希望这在某种程度上也适用于您。 But on CI sometimes use a debugger like GDB or the debugger in Eclipse.
但是在CI上有时会使用GDB等调试器或Eclipse中的调试器。 I also use valgrind (I really like this tool a lot) to fix memory leaks/segmentation faults.
我还使用valgrind (我非常喜欢这个工具)来修复内存泄漏/分段错误。 I advice you to like at this tutorial to get a better understanding of what valgrind can do for you.
我建议您喜欢本教程 ,以更好地了解valgrind可以为您做什么。 Valgrind can do a lot more so I would advice you to read about valgrind/helgrind.
Valgrind可以做更多的事情,所以我建议您阅读有关valgrind / helgrind的文章。
thanks everyone for your answers. 谢谢大家的回答。 The bug was in the piece of code that I didn't paste :( I was passing an instance of RandomNumberGenerator as a normal parameter. When I changed it to passing as reference it started to work magically.
该错误存在于我未粘贴的代码段中:(我正在将RandomNumberGenerator的实例作为常规参数传递。当我将其更改为作为引用传递时,它开始神奇地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.