View Single Post
Old 2012-10-25, 12:46   #1
burrobert
 
burrobert's Avatar
 
Oct 2012
Altona Victoria

148 Posts
Default Using msieve with c

I recently set up gmp, gmp-ecm, ggnfs msieve, and aliquiet on my Ubuntu 12.04 machine. Everything seems to work fine from the command line and the python script factsieve.py does what it should. I decided to try using the msieve functions in a c program but get a segmentation fault. The program works fine on my Macbook under xcode. Here is the program (with a test factorisation of 101101).

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gmp.h>
#include <msieve.h>
#include <sys/times.h>
 
int main (int argc, const char * argv[])
{
msieve_obj *mobj;
char *mint;
char buffer[1000]; 
 
srand((unsigned int) time( NULL ));
mint = "101101";
printf("mint = %s\n", mint);
msieve_factor *f;
mobj = calloc(1, sizeof(msieve_obj));
 
mobj->savefile.name = "/home/msieve_savefile.txt";
mobj->savefile.buf = &buffer[0];
mobj->max_relations = 0;
mobj->input = mint;
mobj->seed1 = rand();
mobj->seed2 = rand();
 
msieve_run(mobj);
f = mobj->factors;
// Makes sure there is a place to start
if (f != 0 ) {
while ( f->next != 0 ) {
printf("%s, %s\n", f->number, (f->factor_type == 0) ? "composite" : (f->factor_type == 1) ? "prime" : "probable prime");
f = f->next;
}
printf("%s, %s\n", f->number, (f->factor_type == 0) ? "composite" : (f->factor_type == 1) ? "prime" : "probable prime");
}
 
return EXIT_SUCCESS;
}
*******************
I get the output:

Code:
mint = 101101
Segmentation fault (core dumped)
I have tracked the problem down to the function msieve_run. Valgrind output is as follows:

Code:
mint = 101101
==2987== Invalid write of size 1
==2987== at 0x8050EDD: mp_print (in /home/rob/Mathwork/msieve/trunk/a.out)
==2987== by 0x804CCC5: msieve_run (in /home/rob/Mathwork/msieve/trunk/a.out)
==2987== by 0x804C061: main (in /home/rob/Mathwork/msieve/trunk/a.out)
==2987== Address 0x400 is not stack'd, malloc'd or (recently) free'd
==2987== 
==2987== 
==2987== Process terminating with default action of signal 11 (SIGSEGV)
==2987== Access not within mapped region at address 0x400
==2987== at 0x8050EDD: mp_print (in /home/rob/Mathwork/msieve/trunk/a.out)
==2987== by 0x804CCC5: msieve_run (in /home/rob/Mathwork/msieve/trunk/a.out)
==2987== by 0x804C061: main (in /home/rob/Mathwork/msieve/trunk/a.out)
I am not sure what might be causing the problems so would be grateful for any suggestions.

Last fiddled with by Batalov on 2012-10-26 at 20:36 Reason: /code/ tags added
burrobert is offline   Reply With Quote