【问题标题】:GMP mpf functions causing a segmentation faultGMP mpf 函数导致分段错误
【发布时间】:2014-08-28 05:48:59
【问题描述】:

我无法弄清楚导致此错误的原因。我刚刚在ubuntu上安装了GMP。这是 AMD cpu 上的 64 位操作系统(不确定是否重要)。我不断收到分段错误。

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


int main(int argc, char** argv)
{
    mpz_t sum, fac;
    mpf_t fsum, ffac;
    int i;
    time_t t;

    mpz_init_set_ui(sum, 1);
    mpz_init_set_ui(fac, 1);

    t = time(NULL);

    for(i = 10000; i >= 1; --i)
    {
    mpz_mul_ui(fac, fac, i);
    mpz_add(sum, sum, fac);
    if(i % 10000 == 0)
    {
        printf("%d\n", i);
    }
    }

    printf("Time %d\n", (time(0) - t));

    mpf_init(fsum);
    mpf_init(ffac);
    mpf_set_z(fsum, sum);
    mpf_set_z(ffac, fac);


    mpz_clear(sum);
    mpz_clear(fac);

    mpf_div(fac, sum, fac);

    mpf_out_str(stdout, 10, 50, fac);

    mpf_clear(fsum);
    mpf_clear(ffac);
    return(EXIT_SUCCESS);
}

此代码输出以下内容...

10000
Time 0
Segmentation fault (core dumped)

然后我尝试用 valgrind 运行这个程序,这是输出。

==25427== Memcheck, a memory error detector
==25427== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==25427== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==25427== Command: /home/chase/NetBeansProjects/GmpECalc/dist/Debug/GNU-Linux-x86/gmpecalc
==25427== 
10000
Time 1
==25427== Invalid read of size 8
==25427==    at 0x4E8E590: __gmpn_copyi (in /usr/lib/x86_64-linux-gnu/libgmp.so.10.1.3)
==25427==    by 0x400B27: main (main.c:40)
==25427==  Address 0x73b0000073c is not stack'd, malloc'd or (recently) free'd
==25427== 
==25427== 
==25427== Process terminating with default action of signal 11 (SIGSEGV)
==25427==  Access not within mapped region at address 0x73B0000073C
==25427==    at 0x4E8E590: __gmpn_copyi (in /usr/lib/x86_64-linux-gnu/libgmp.so.10.1.3)
==25427==    by 0x400B27: main (main.c:40)
==25427==  If you believe this happened as a result of a stack
==25427==  overflow in your program's main thread (unlikely but
==25427==  possible), you can try to increase the size of the
==25427==  main thread stack using the --main-stacksize= flag.
==25427==  The main thread stack size used in this run was 8388608.
==25427== 
==25427== HEAP SUMMARY:
==25427==     in use at exit: 48 bytes in 2 blocks
==25427==   total heap usage: 3,706 allocs, 3,704 frees, 27,454,096 bytes allocated
==25427== 
==25427== LEAK SUMMARY:
==25427==    definitely lost: 0 bytes in 0 blocks
==25427==    indirectly lost: 0 bytes in 0 blocks
==25427==      possibly lost: 0 bytes in 0 blocks
==25427==    still reachable: 48 bytes in 2 blocks
==25427==         suppressed: 0 bytes in 0 blocks
==25427== Rerun with --leak-check=full to see details of leaked memory
==25427== 
==25427== For counts of detected and suppressed errors, rerun with: -v
==25427== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

错误似乎发生在 mpf_div 函数中。但是,如果我删除此函数,则会在 mpf_out_str 处发生错误。我还尝试将 ffac 和 fsum 初始化为双精度数(而不是将它们设置为 fac 和 sum),我得到了同样的错误。

【问题讨论】:

    标签: c++ c linux segmentation-fault gmp


    【解决方案1】:

    问题出在这几行:

    mpz_clear(sum); // You clear the variables, GMP deallocates their memory
    mpz_clear(fac);
    
    mpf_div(fac, sum, fac); // You use cleared variables, segfault
    

    也许你的意思是:

    mpf_div(ffac, fsum, ffac); 
    

    【讨论】:

    • 谢谢。在盯着代码一个小时后,我实际上设法自己弄清楚了。
    猜你喜欢
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2011-01-08
    • 2016-01-07
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多