Using a pre-computed array in another function?

Gabriel Risterucci cleyfaye at gmail.com
Sun Jul 8 10:50:51 CEST 2012


2012/7/8 Alasdair McAndrew <amca01 at gmail.com>

> Hi there,
>
> I have a pre-computed array, which I'd like to use in another function.
>  This program shows the sort of thing I'm trying to do - I've created an
> array A in the main program, and I need to use those values in my function
> "mpz_myfun".  However, this gives me a segmentation fault.
>
> Any ideas as to how I can fix this up so it works?  Oh, I'm using gcc
> version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) and gmp 5.0.5.
>
> Thanks,
> Alasdair
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <gmp.h>
>
> mpz_t b,r,j,p,T,n,out;
> mpz_t *A;
>
> void mpz_myfun(mpz_t r,mpz_t m)
> {
>   gmp_printf("Zd\n",A[100]);
>   mpz_t tmp;
>   mpz_init(tmp);
>   mpz_and(tmp,m,T);
>   mpz_set(r,A[mpz_get_ui(tmp)]);
>   mpz_clear (tmp);
> }
>
> int main(int argc, char *argv[])
> {
>    mpz_init_set_ui(b,7);
>    mpz_init_set_ui(T,65535);
>    mpz_init_set_ui(p,0x7FFFFFFF);
>    mpz_init(j);
>    mpz_t *A = malloc(65536*sizeof(mpz_t));
>    for (unsigned long i=0; i<65536; i++)
>      {
>        mpz_powm_ui(A[i],b,i,p);
>      }
>
>    mpz_init_set_str(n,argv[1],10);
>    mpz_init(out);
>
>    mpz_myfun(out,n);
>
>    gmp_printf("%ZD\n",out);
>
>    return 0;
> }
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
>

You seem to be missing to call mpz_init() on all of your array values. Your
malloc only allocate space for the mpz_t variables, but you should
initialize them like any other mpz_t.


More information about the gmp-discuss mailing list