我正在审核此代码:
#include <stdio.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_permutation.h>
int main ( ) {
const long N = 10;
const gsl_rng_type *T = NULL;
gsl_rng *r = NULL;
gsl_permutation *p = NULL;
p = gsl_permutation_alloc ( N );
gsl_permutation *q = NULL;
q = gsl_permutation_alloc ( N );
gsl_rng_env_setup ();
T = gsl_rng_default;
r = gsl_rng_alloc ( T );
printf ( "initial permutation: " );
gsl_permutation_init ( p );
gsl_permutation_fprintf ( stdout, p, " %u" );
printf ( "\n" );
printf (" random permutation:");
gsl_ran_shuffle (r, p->data, N, sizeof(size_t));
gsl_permutation_fprintf (stdout, p, " %u");
printf ("\n");
printf ( "inverse permutation: " );
gsl_permutation_inverse ( q, p );
gsl_permutation_fprintf ( stdout, q, "%u" );
printf ( "\n" );
gsl_permutation_free ( p );
gsl_permutation_free ( q );
return 0;
}
当我尝试编译它时,我得到未处理的异常:访问冲突写入位置错误。
我相信,我为所有指针分配了内存但是,错误仍然存在。
我不太熟悉C所以,任何输入都将不胜感激。
附:当我使用调试器时,它指向这一行:gsl_permutation_fprintf ( stdout, p, " %u" );
感谢
答案 0 :(得分:0)
不应该首先调用gsl_rng_env_setup ();
吗?你以前做了几个gsl_permutation_alloc
。