开发者

Access violation writing location error

I am reviewing this code here:

#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 ( 开发者_Python百科q, p );
    gsl_permutation_fprintf ( stdout, q, "%u" );
    printf ( "\n" );

    gsl_permutation_free ( p );
    gsl_permutation_free ( q );

    return 0;
}

When I try to compile it however, I get unhandled exception: Access violation writing location error. I believe, I allocated memory for all the pointers and yet, the error persists. I'm not too familiar with C so, any input will be appreciated. P.S. When I use debugger, it points to this line: gsl_permutation_fprintf ( stdout, p, " %u" ); Thanks

EDIT: I've played around with the code and realized that the problem could be in stdout statement there.


Shouldn't gsl_rng_env_setup (); be called very first? you do a couple of gsl_permutation_alloc before.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜