简体   繁体   English

std::uniform_int_distribution 的分段错误

[英]Segmentation fault with std::uniform_int_distribution

I have a program containing multiple classes, some of which require random doubles and ints.我有一个包含多个类的程序,其中一些需要随机双精度和整数。 In one of the classes, I defined a struct to seed the random engine and be able to generate random reals and ints through an object of that struct whenever I need one.在其中一个类中,我定义了一个结构来播种随机引擎,并能够在需要时通过该结构的 object 生成随机实数和整数。 The.hpp file with class and struct declations looks like this:带有 class 和结构声明的 .hpp 文件如下所示:

struct RNG {
public:
    static std::mt19937 gen;
    static std::uniform_int_distribution<uint32_t> dist_ui;
    static std::uniform_real_distribution<double> dist_d;
    uint32_t r_ui = dist_ui(gen);
    double r_d = dist_d(gen);
private:
    static unsigned seed;
};

class A {
private:
    uint32_t a_;
public:
    A();
};

The.cpp file looks like this: .cpp 文件如下所示:

#include "A.hpp"

unsigned RNG::seed = 42;
std::mt19937 RNG::gen(RNG::seed);
std::uniform_int_distribution<uint32_t> RNG::dist_ui(1, std::numeric_limits<uint32_t>::max());
std::uniform_real_distribution<double> RNG::dist_d(0.0,1.0);

A::A() {
    RNG* r;
    a_ = r->dist_ui(r->gen);
}

Now, if I call uniform_real_distribution with现在,如果我调用 uniform_real_distribution

r->dist_d(r->gen)

everthing works fine.一切正常。 However, if I call uniform_int_distribution with但是,如果我调用 uniform_int_distribution

r->dist_ui(r->gen)

as in the above snippet, I get a segmentation fault.如上面的片段,我得到一个分段错误。 The error also occurs if I define my struct only with int dist instead of both real and int, or if I change the boundaries of the int dist to [0,100) or whatever.如果我只使用 int dist 而不是 real 和 int 定义我的结构,或者如果我将 int dist 的边界更改为 [0,100) 或其他什么,也会发生错误。 Does anyone have a clue what happens here?有谁知道这里发生了什么? I appreciate any help!我很感激任何帮助!

EDIT: I get the same error if I access the non-static members like this编辑:如果我像这样访问非静态成员,我会得到同样的错误

RNG r;
a_ = r.r_ui;

I have a guess here.我在这里有一个猜测。 Is the object of type A also static/global? A型的object也是静态/全局的吗? If so, maybe your problem is because the order of static initialization is not defined.如果是这样,可能你的问题是因为 static 初始化的顺序没有定义。 So, you're accessing dist_ui before it's initialized.因此,您在 dist_ui 初始化之前访问它。

Please post the rest of the code (the one that creates an instance of A ) if it's not the case.如果不是这种情况,请发布代码的 rest (创建A实例的代码)。

A minimal reproducible example worked fine, in the end, I resolved the problem by carefully rebuilding my program.一个最小的可重现示例运行良好,最后,我通过仔细重建我的程序解决了这个问题。 Doing so, I found wrong access of an std::vector, which was probably one of the reasons execution failed.这样做,我发现 std::vector 的访问错误,这可能是执行失败的原因之一。 So, long story short: I messed up my code and didn't look carefully enough before posting this question.所以,长话短说:我搞砸了我的代码,在发布这个问题之前没有仔细看。 However, as pointed out in the comments, I should not access uninitialized pointers as I am doing in my snippet below ( RNG* r ) even if they're static.但是,正如评论中所指出的,我不应该像在下面的代码段( RNG* r )中那样访问未初始化的指针,即使它们是 static。 Instead, use the scope resolution operator (::)而是使用 scope 分辨率运算符 (::)

a_ = RNG::dist_ui(RNG::gen)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM