2019-04-23, 17:49 | #1 |
"Ed Hall"
Dec 2009
Adirondack Mtns
2^{2}·19·43 Posts |
GMP mpz_set_ui Troubles
I'm programming this in C++ using Code::Blocks, which I've used for many other GMP programs.
Can anyone tell me why tf2 cannot be set, when it is initialized identically to tf1? Overall program: Code:
#include <iostream> #include <gmp.h> using namespace std; int main(int argc, char **argv) { mpz_t comp,zero,one,ten,factor1,factor2,tf1[500],tf2[500],tc1,tc2,testcomp,mplier[500]; mpz_inits(comp,zero,one,ten,factor1,factor2,tf1[500],tf2[500],tc1,tc2,testcomp,mplier[500],NULL); unsigned long int a, b, c, i[1000], j, k; int check; char composite1[] = "161"; //test case (23*7) char composite2[] = "996685583"; // test case (11579*86077) char composite3[] = "4033064567209"; // test case (1535441*2626649) char composite4[] = "80246333911201573"; // test case (377060293*212820961) char composite5[] = "35051259095366121511"; //test case (377060293*92959295227) char composite6[] = "600215441637324288364976551"; // test case (10092006740707*59474340144493) mpz_set_str(comp,composite2,10); // initialize gmp composite mpz_set_str(zero,"0", 10); // initialize a gmp variable equal to 0 mpz_set_str(one,"1", 10); // initialize a gmp variable equal to 1 mpz_set_str(ten,"10", 10); // initialize a gmp variable equal to 10 cout<<"\nThe composite is: "; mpz_out_str(stdout, 10, comp); // print value of composite cout<<endl<<endl; mpz_set(mplier[0],one); for(a=1;a<20;a++){ mpz_mul(mplier[a],mplier[a-1],ten); } for(i[0]=0;i[0]<10;i[0]++){ mpz_set_ui(tf1[0],i[0]); mpz_set_ui(tf2[0],i[0]); cout<<"tf1[0] is "; mpz_out_str(stdout, 10, tf1[0]); cout<<endl; } return 0; } Code:
The composite is: 996685583 tf1[0] is 0 tf1[0] is 1 tf1[0] is 2 tf1[0] is 3 tf1[0] is 4 tf1[0] is 5 tf1[0] is 6 tf1[0] is 7 tf1[0] is 8 tf1[0] is 9 Process returned 0 (0x0) execution time : 0.004 s Press ENTER to continue. Code:
The composite is: 996685583 Process returned -1 (0xFFFFFFFF) execution time : 0.078 s Press ENTER to continue. I have also swapped the bold line with the one above and the bold line still causes the trouble. Also, I get no compile errors, only warnings about the unused variables. |
2019-04-23, 17:54 | #2 |
Just call me Henry
"David"
Sep 2007
Cambridge (GMT)
2^{6}×89 Posts |
I can't see any issue. Have you tried compiling with -Wall?
edit: Is mpz_inits designed to work with arrays of mpz_t? I suspect not and memory corruption is causing your issues; Last fiddled with by henryzz on 2019-04-23 at 18:03 |
2019-04-23, 18:02 | #3 |
"Ben"
Feb 2007
2^{2}×3^{2}×7×13 Posts |
I have never used mpz_inits, but after reading about it, I would not expect it to know that tf1 or tf2 are length 500 arrays.
If I were me, I'd use a loop to initialize the mpz_t arrays one element at a time. |
2019-04-23, 18:21 | #4 |
Sep 2002
Database er0rr
3336_{10} Posts |
Have you tried mpz_inits without the array definition, which after all are declared in the mpz_t line?, i.e. just tf1 and tf2 in mpz_inits.
Last fiddled with by paulunderwood on 2019-04-23 at 18:21 |
2019-04-23, 18:51 | #5 |
"Ed Hall"
Dec 2009
Adirondack Mtns
2^{2}×19×43 Posts |
Thanks guys! It was the array portion of the mpz_inits. All seems well now with just tf1 and tf2 in that line. (I also removed the array portion from mplier.)
I will be setting each tf1[] and tf2[] prior to use, so if they aren't truly zeroed, I should still be OK at this point. If I need, I'll go ahead and use a loop later. I appreciate the help. |
2019-04-23, 19:57 | #6 |
"Ed Hall"
Dec 2009
Adirondack Mtns
2^{2}×19×43 Posts |
Just for completeness, I did have to initialize all the elements individually:
Code:
for(a=0;a<500;a++){ mpz_init(tf1[a]); mpz_init(tf2[a]); mpz_init(mplier[a]); } |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Apparent Ubuntu Version Troubles with ecmpi | EdH | GMP-ECM | 1 | 2020-05-04 13:16 |
mpz_aprcl.h and jacobi_sum.h Troubles with r343 | EdH | YAFU | 2 | 2015-11-03 13:04 |
[URGENT] Pain: troubles on implementing SIQS sieve | Hermes | Factoring | 27 | 2008-10-14 13:54 |
Troubles with Debian Netinst | ET_ | Linux | 4 | 2007-03-13 20:41 |