c - 为什么使用 GMP 时出现分段错误?
问题描述
我正在使用GMP。我的程序可以成功构建,但运行失败。以下是错误的事情:
a=1231231231231231
res^n != a
Segment fault
我程序中的所有代码都是:
#include <gmp.h>
#include <stdio.h>
int main()
{
mpz_t a,res;
unsigned long int n = 123;
char str1[] = "1231231231231231";
mpz_init_set_str(a, str1, 10);
gmp_printf("a=%Zd\n",a);
mpz_init(res);
if(mpz_root(res, a, n)){
printf("res^n == a\n");
}
else{
printf("res^n != a\n");
}
mpz_clears(a,res);
return 0;
}
解决方案
你必须像这样调用 mpz_clears() :
mpz_clears(a,res, NULL);
这是文档中的内容:
Function: void mpz_clears (mpz_t x, ...)
Free the space occupied by a NULL-terminated list of mpz_t variables.
推荐阅读
- java - 如何在 JAVA 上运行的协议缓冲区 3 中显示默认值或字段
- javafx - 在两个不同大小的屏幕上运行应用程序时调整 JavaFX 窗口大小
- docker - 当基本映像是 centos vs ubuntu:trusty 时,当以 shell 形式运行 CMD/ENTRYPOINT 时,不同的进程以 PID 1 运行
- exist-db - 动态页面的 eXist-db URL 映射
- linux - crontab - 什么都没有发生
- javascript - 在地图迭代中添加额外的组件会出错
- eclipse - 运行外部工具后 Eclipse 刷新工作区
- python - (Python) Networkx - 根据值更改节点大小
- excel - 在列中最后使用的单元格之后插入公式的宏
- c - “mov %eax,%db0”处的分段错误