Weird segmentation fault error
Sumeet
sumeetsk at gmail.com
Tue Jan 10 11:28:19 CET 2012
I'm getting a weird segmentation fault error.
The following program works fine:
//====================================================================
#include <stdio.h>
#include <gmp.h>
void main() {
mpf_set_default_prec(100);
mpf_t s1,s2,s3,s4,a1,a2,a3,a4,a5,s,days;
mpz_t ans;
int i;
mpf_inits(s1,s2,s3,s4,a1,a2,a3,a4,a5,s,days);
mpz_init(ans);
//for (i = 0; i < 20; i++) {
i = 16;
mpf_sqrt_ui(a1,2); //sqrt(2)
mpf_mul_ui(a2,a1,2); //2sqrt(2)
mpf_add_ui(a3,a2,3); //3 + 2sqrt(2)
mpf_ui_sub(a4,3,a2); //3 - 2sqrt(2)
mpf_mul_ui(a5,a1,4); //4*sqrt(2)
mpf_pow_ui(s1,a3,i);
mpf_pow_ui(s2,a4,i);
mpf_sub(s3,s1,s2);
mpf_div(s4,s3,a5);
mpf_pow_ui(s,s4,2);
mpf_add_ui(s,s,1);
mpz_set_f(ans,s);
mpf_div_ui(days,s,86400);
mpz_out_str(stdout, 10, ans);
printf("\n");
mpf_out_str(stdout,10,30,days);
printf("\n");
//}
mpf_clears(s1,s2,s3,s4,a1,a2,a3,a4,a5,s,days);
mpz_clear(ans);
}
//===============================================================
However, when I add just one more mpz_t variable, I get a segmentation
fault:
//===============================================================
#include <stdio.h>
#include <gmp.h>
void main() {
mpf_set_default_prec(100);
mpf_t s1,s2,s3,s4,a1,a2,a3,a4,a5,s,days;
mpz_t ans,ans1; // <<====== ONLY ADDED ans1
int i;
mpf_inits(s1,s2,s3,s4,a1,a2,a3,a4,a5,s,days);
mpz_init(ans);
//for (i = 0; i < 20; i++) {
i = 16;
mpf_sqrt_ui(a1,2); //sqrt(2)
mpf_mul_ui(a2,a1,2); //2sqrt(2)
mpf_add_ui(a3,a2,3); //3 + 2sqrt(2)
mpf_ui_sub(a4,3,a2); //3 - 2sqrt(2)
mpf_mul_ui(a5,a1,4); //4*sqrt(2)
mpf_pow_ui(s1,a3,i);
mpf_pow_ui(s2,a4,i);
mpf_sub(s3,s1,s2);
mpf_div(s4,s3,a5);
mpf_pow_ui(s,s4,2);
mpf_add_ui(s,s,1);
mpz_set_f(ans,s);
mpf_div_ui(days,s,86400);
mpz_out_str(stdout, 10, ans);
printf("\n");
mpf_out_str(stdout,10,30,days);
printf("\n");
//}
mpf_clears(s1,s2,s3,s4,a1,a2,a3,a4,a5,s,days);
mpz_clear(ans);
}
//===============================================================
I tried clearing the previous variables before declaring ans1 (since I
thought the error may be due to low memory), but that didn't help.
Could somebody point out the reason for the segfault error?
Thanks in advance,
Sumeet.
More information about the gmp-discuss
mailing list