std :: uniform_int_distribution的分段错误 [英] Segmentation fault with std::uniform_int_distribution

查看:322
本文介绍了std :: uniform_int_distribution的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑编辑:最小的可复制示例效果很好,最后,我通过仔细重建程序解决了该问题。这样做时,我发现对std :: vector的访问错误,这可能是执行失败的原因之一。
长话短说:我记了我的代码,在发布此问题之前看起来不够仔细。
但是,正如评论中所指出的那样,我不应该像下面的代码段那样访问未初始化的指针( RNG * r )静态的。而是使用范围解析运算符(::)

EDIT A minimal reproducible example worked fine, in the end, I resolved the problem by carefully rebuilding my program. Doing so, I found a wrong access of an std::vector, which was probably one of the reasons execution failed. So, long story short: I meessed 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. Instead, use the scope resolution operator (::)

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






我有一个包含多个类的程序,其中一些需要随机双打和整数。在其中一个类中,我定义了一个结构以植入随机引擎,并在需要时可以通过该结构的对象生成随机实数和整数。
具有类和结构修饰符的.hpp文件如下所示:


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. The .hpp file with class and struct declations looks like this:

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();
};

.cpp文件如下所示:

The .cpp file looks like this:

#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);
}

现在,如果我用

r->dist_d(r->gen)

一切正常。但是,如果我使用



everthing works fine. However, if I call uniform_int_distribution with

r->dist_ui(r->gen)

如上面的代码片段所示,我遇到了分段错误。如果仅使用int dist而不是real和int定义结构,或者将int dist的边界更改为[0,100)或其他任何值,也会发生错误。
有人知道这里会发生什么吗?感谢您的帮助!

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. Does anyone have a clue what happens here? I appreciate any help!

编辑:
如果访问像这样的非静态成员,则会遇到相同的错误

I get the same error if I access the non-static members like this

RNG r;
a_ = r.r_ui;


推荐答案

我在这里有一个猜测。 A 类型的对象也是静态/全局对象吗?如果是这样,可能是您的问题是因为未定义静态初始化的顺序。因此,您需要在dist_ui初始化之前访问它。

I have a guess here. Is the object of type A also static/global? If so, maybe your problem is because the order of static initialization is not defined. So, you're accessing dist_ui before it's initialized.

请发布其余代码(创建 A实例的代码)。

Please post the rest of the code (the one that creates an instance of A) if it's not the case.

这篇关于std :: uniform_int_distribution的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆