gpt4 book ai didi

c - 使用 mpz_set_str 初始化两个以上的 mpz_t 会导致段错误

转载 作者:行者123 更新时间:2023-11-30 14:52:10 24 4
gpt4 key购买 nike

有人知道为什么第二次调用 mpz_set_str() 后会导致段错误吗?如何从 str 初始化两个以上的 gmp int?

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

int main(int argc, char *argv[]) {

mpz_t a, b, c;

mpz_set_str(a, "10", 10);
printf("gets here a\n");
mpz_set_str(b, "20", 10);
printf("gets here b\n");
mpz_set_str(c, "30", 10);
printf("gets here c\n");
}

编译为:gcc -lm -lgmp -o segf segf.c

最佳答案

documentation for mpz_set_str说:

5.2 Assignment Functions

These functions assign new values to already initialized integers (see Initializing Integers).

...

链接转到

5.1 Initialization Functions

The functions for integer arithmetic assume that all integer objects are initialized. You do that by calling the function mpz_init. For example,

{
mpz_t integ;
mpz_init (integ);

mpz_add (integ, …);

mpz_sub (integ, …);

/* Unless the program is about to exit, do ... */
mpz_clear (integ);
}

As you can see, you can store new values any number of times, once an object is initialized.

您的代码不会初始化变量,因此诸如 mpz_set_str 之类的赋值函数会产生垃圾。

关于c - 使用 mpz_set_str 初始化两个以上的 mpz_t 会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47733035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com