"中止"程序使用GNU MP"大数字"图书馆

时间:2014-10-23 22:35:37

标签: c gmp

我正在尝试使用大数字在C中实现以下(伪代码python):

while i * i < n:
    if n % i:
    i += 1

我使用gmp.h库找到了以下内容:

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

int main () {
    mpz_t n;
    mpz_t i;
    mpz_t o; // Hold constant mpz "1" for incrementing i
    mpz_t x; // Holds "i * i" each loop

    mpz_set_str(n, "338852330881981183", 10);
    mpz_set_str(i, "2", 10);
    mpz_set_str(o, "1", 10);
    mpz_set_str(x, "4", 10);


    while( mpz_cmp(x, n) < 0 ) {
        mpz_add(i, i, o);
        mpz_pow_ui(x, i, 2);
    }

    mpz_clear(i);
    mpz_clear(n);
    return 0;
}

我在使用GCC编译时没有出现任何错误,但是当我尝试运行该程序时,我得到以下Aborted。我以前从未见过这个,也没有在C中编程很多,更不用说使用gmp.h和非常大的数字了。

*** glibc detected *** ./factor: realloc(): invalid old size: 0x00000000004006d0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x76d76)[0x7fdc1eb7bd76]
/lib/x86_64-linux-gnu/libc.so.6(+0x7c94c)[0x7fdc1eb8194c]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0xf0)[0x7fdc1eb81c60]
/usr/lib/x86_64-linux-gnu/libgmp.so.10(__gmp_default_reallocate+0x1c)[0x7fdc1ee98a1c]
/usr/lib/x86_64-linux-gnu/libgmp.so.10(__gmpz_realloc+0x3a)[0x7fdc1eeaebba]
/usr/lib/x86_64-linux-gnu/libgmp.so.10(__gmpz_set_str+0x301)[0x7fdc1eeafef1]
./factor[0x400810]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd)[0x7fdc1eb23ead]
./factor[0x4006f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 1723311                            /root/Desktop/factor
00600000-00601000 rw-p 00000000 08:01 1723311                            /root/Desktop/factor
01629000-0164a000 rw-p 00000000 00:00 0                                  [heap]
7fdc18000000-7fdc18021000 rw-p 00000000 00:00 0 
7fdc18021000-7fdc1c000000 ---p 00000000 00:00 0 
7fdc1e8ef000-7fdc1e904000 r-xp 00000000 08:01 1445725                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdc1e904000-7fdc1eb04000 ---p 00015000 08:01 1445725                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdc1eb04000-7fdc1eb05000 rw-p 00015000 08:01 1445725                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdc1eb05000-7fdc1ec85000 r-xp 00000000 08:01 1445701                    /lib/x86_64-linux-gnu/libc-2.13.so
7fdc1ec85000-7fdc1ee85000 ---p 00180000 08:01 1445701                    /lib/x86_64-linux-gnu/libc-2.13.so
7fdc1ee85000-7fdc1ee89000 r--p 00180000 08:01 1445701                    /lib/x86_64-linux-gnu/libc-2.13.so
7fdc1ee89000-7fdc1ee8a000 rw-p 00184000 08:01 1445701                    /lib/x86_64-linux-gnu/libc-2.13.so
7fdc1ee8a000-7fdc1ee8f000 rw-p 00000000 00:00 0 
7fdc1ee8f000-7fdc1eef6000 r-xp 00000000 08:01 2240985                    /usr/lib/x86_64-linux-gnu/libgmp.so.10.0.5
7fdc1eef6000-7fdc1f0f6000 ---p 00067000 08:01 2240985                    /usr/lib/x86_64-linux-gnu/libgmp.so.10.0.5
7fdc1f0f6000-7fdc1f0fe000 rw-p 00067000 08:01 2240985                    /usr/lib/x86_64-linux-gnu/libgmp.so.10.0.5
7fdc1f0fe000-7fdc1f11e000 r-xp 00000000 08:01 1445677                    /lib/x86_64-linux-gnu/ld-2.13.so
7fdc1f2fb000-7fdc1f2fe000 rw-p 00000000 00:00 0 
7fdc1f31b000-7fdc1f31d000 rw-p 00000000 00:00 0 
7fdc1f31d000-7fdc1f31e000 r--p 0001f000 08:01 1445677                    /lib/x86_64-linux-gnu/ld-2.13.so
7fdc1f31e000-7fdc1f31f000 rw-p 00020000 08:01 1445677                    /lib/x86_64-linux-gnu/ld-2.13.so
7fdc1f31f000-7fdc1f320000 rw-p 00000000 00:00 0 
7fffa2304000-7fffa2325000 rw-p 00000000 00:00 0                          [stack]
7fffa23b2000-7fffa23b4000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

1 个答案:

答案 0 :(得分:5)

你应initialize使用mpz_init的GMP大整数。因此,main函数应以

开头
mpz_t n;
mpz_t i;
mpz_t o; // Hold constant mpz "1" for incrementing i
mpz_t x; // Holds "i * i" each loop

mpz_init(n);
mpz_init(i);
mpz_init(o);
mpz_init(x);

顺便说一句,您可能希望x main附近mpz_out_str (stdout, 10, x); 使用

mpz_clear
在致电gcc -Wall -Wextra -g yourprog.c -lgmp -o yourprog

之前

然后你应该使用-O2 mtune=native进行编译。正确调试程序后,您可以添加{{1}}来优化程序。