mpq_t array in sub-functions

E Lau elau@hotmail.com
Tue, 11 Mar 2003 00:25:22 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_4869_46e9_5e3a
Content-Type: text/plain; format=flowed

Hi all:

I am new to GMP, so I was wondering if someone can give me a hint here.  I 
was trying to define an array in mpq_t.  It works fine when I define this 
array in the main function.  However, when I define this array in a sub 
function, the program generate seg fault.  I attached the sample C file 
here.  The function "void assignValue(mpq_t x[], int size)" contains 4 
commented-out lines of code which are causing seg faults in the simple test 
program.

I'm open to comments and suggestions.  Thanks.


Sincerely,

Edmond Lau
===============================
http://www.maximalgorithms.com/



_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online  
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

------=_NextPart_000_4869_46e9_5e3a
Content-Type: text/plain; name="test_gmp.c"; format=flowed
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="test_gmp.c"

#include <stdio.h>
#include <gmp.h>

/*
* initialize Polynomial
*/
void initPoly(mpq_t **x, int size){
   int i;
   *x = (mpq_t*) malloc(size*sizeof(mpq_t));
   for (i = 0; i < size; i++){
      mpq_init((*x)[i]);
   }
}

/*
* clear polynomial
*/
void clearPoly(mpq_t x[], int size){
   int i;
   for (i = 0; i < size; i++){
      mpq_clear(x[i]);
   }
   free(x);
}

/*
* plus 2
*/
void assignValue2(mpq_t x[], int size){
   int i;
   mpq_t two;
   mpq_init(two);
   mpq_set_si(two,2,1);
   for (i = 0; i < size; i++){
      mpq_add(x[i],x[i],two);
   }
   mpq_clear(two);
}

/*
* plus 3
*/

void assignValue3(mpq_t x[], int size){
   int i;
   mpq_t three;
   mpq_init(three);
   mpq_set_si(three,3,1);
   for (i = 0; i < size; i++){
      mpq_add(x[i],x[i],three);
   }
   mpq_clear(three);
}

void assignValue(mpq_t x[], int size){
//   mpq_t *R;
//   initPoly(&R,15);

//   assignValue2(R,size);
   assignValue3(x,size);

//   clearPoly(R,15);
}

/*
* display value in polynomial
*/
void outputValue(mpq_t x[], int size){
   int i;

   for (i = 0; i < size; i++){
      mpq_out_str(stdout,10,x[i]);
      printf("\n");
   }
}

int main(void){

   mpq_t *A,*B;
   initPoly(&A, 10);
   initPoly(&B, 20);

   assignValue(A,10);
   assignValue(A,5);
   assignValue(B,20);
   assignValue(B,20);
   assignValue(B,20);

   outputValue(A,10);
   printf("\n===========\n");
   outputValue(B,20);

   clearPoly(A,10);
   clearPoly(B,20);
   return(0);
}


------=_NextPart_000_4869_46e9_5e3a--