Need help to getting started with GMP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
#include <gmp.h>
#include <stdlib.h>
#include <string>
using namespace std;

void* asgn(mpf_t* ptr, string str)
{
    ptr = (mpf_t*)malloc(16);
    mpf_set_prec(*ptr, str.length());
    mpf_set_str(*ptr, str.c_str(), 10);
}


int main()
{
    mpf_t *fl1;
    asgn(fl1, "123.45678954");

    return 0;
}


when i compile and run the code, the programme stop working immediately. I suspect that i might have failed at memory allocation part but i have no idea how to deal with it.

And does the size of mpf_t in bytes is depends on the size of the string?
Based on namespace and <srting>, you're using C++, so consider C++ interface to GMP:

1
2
3
4
5
#include <gmpxx.h>
int main()
{
    mpf_class fl1("123.45678954");
}
Last edited on
That works well enough, Thanks

Topic archived. No new replies allowed.