使用gsl_rng_uniform时出现分段错误

时间:2012-03-29 02:36:23

标签: c++ segmentation-fault gsl

我正在编写一个使用GSL随机数生成器的程序,当我尝试将随机数生成器的实例传递给函数时,我遇到了分段错误。这是我的源代码:

int main(void)
{
    gsl_rng *r;
    int deck[52];
    int count = 0;

    r = gsl_rng_alloc(gsl_rng_mt19937);
    gsl_rng_set(r, time(NULL));

    // Initialize a custom deck 
    // code omitted...    
    // Perform trials
    for (int j = 0; j < NUMTRIALS; j++) {
        shuffle_two(r, deck);
        if (deck[NUMCARDS-1] + deck[NUMCARDS-2] == 11)
            count++;
    }

    // Report result
    cout << fixed << setprecision(6) << count/static_cast<double>(NUMTRIALS);
    cout << endl;

    gsl_rng_free(r);
}

void shuffle_two(gsl_rng* r, int deck[])
{
    double u;
    int bottom, random;
    int temp_card;

    for (int i = 0; i < 2; i++) {
        u = gsl_rng_uniform(r);
        //code for shuffling goes here
    }
}

显然,当算法运行时,r的值会发生变化。当我做回溯时,我得到r有时为null,有时为0xa。我不知道为什么。我认为它可能与gsl_rng_uniform function, as documented here的const指针参数有关。

以下是调试器的输出:

Program received signal SIGSEGV, Segmentation fault.
gsl_rng_uniform (r=0x0) at ../gsl/gsl_rng.h:167
167 ../gsl/gsl_rng.h: No such file or directory.
    in ../gsl/gsl_rng.h
(gdb) backtrace
#0  gsl_rng_uniform (r=0x0) at ../gsl/gsl_rng.h:167
#1  0x0000000000400d97 in shuffle_two (r=0x0, deck=0x7fffffffdfd0)
    at blackjack.cpp:55
#2  0x0000000000400cad in main () at blackjack.cpp:33
(gdb)

0 个答案:

没有答案