In the library FLINT, we have the function fmpz_poly_clear to clear a fmpz_poly polynomial from the memory. But I am using the C++ interface of this library, so, my polynomials are of the type fmpz_polyxx or fmpz_mod_polyxx.
I would like to know how to clear these polynomials. I already searched on the FLINT's documentation, but I found nothing.
For instance, the following program creates a polynomial with seven million coefficients. On my machine, it uses about 1GB of RAM. After exiting the for loop, I try to delete the polynomial.
I am reading a integer after just to make the program wait before terminating the execution.
#include <iostream>
#include "flint/fmpzxx.h"
#include "flint/fmpz_mod_polyxx.h"
#include "flint/fmpz_mod_poly_factorxx.h"
using namespace flint;
int main(int argc, char *argv[])
{
// q (273-bit prime)
fmpzxx q("15177100720513508366558296147058741458143803430094840009779784451085189728165691397");
fmpz_mod_polyxx* myPoly = new fmpz_mod_polyxx(q);
for (int i = 0; i < 7000000; i++){
myPoly->set_coeff(i, q - fmpzxx(i));
}
delete myPoly; // <-- this line does not work
// fmpz_poly_clear(myPoly); // <-- this line does not work
// fmpz_mod_polyxx_clear(myPoly); // <-- this one neither
int a;
std::cin >> a; // wait
return EXIT_SUCCESS;
}
Does someone know how to do it?
When using flint
to work with polynomials, it is best to work with fmpz_mod_poly_t
instead of * fmpz_mod_poly
. The type fmpz_mod_poly_t
is typedefed to an array of length 1 of fmpz_mod_poly_struct
, so it behaves similar to pointers and allows passing parameters of type fmpz_mod_poly_struct
by reference.
(This pattern is held throughout flint
, arb
, calcium
, and the other libraries in the same family). The init
and clear
functions expect these types and will do it appropriately.
For example, this smaller code initializes a polynomial, squares it, prints it, and then clears the memory for both the polynomials and the modulus.
#include "fmpz_mod_poly.h"
int main() {
fmpz_t n; // array of length 1 fmpz_struct
fmpz_mod_poly_t x, y; // array of length 1 fmpz_mod_poly_struct
fmpz_init_set_ui(n, 7); // n = 7
fmpz_mod_poly_init(x, n);
fmpz_mod_poly_init(y, n);
fmpz_mod_poly_set_coeff_ui(x, 3, 5);
fmpz_mod_poly_set_coeff_ui(x, 0, 6);
fmpz_mod_poly_sqr(y, x);
fmpz_mod_poly_print(x); flint_printf("\n");
fmpz_mod_poly_print(y); flint_printf("\n");
fmpz_mod_poly_clear(x); // clear x memory
fmpz_mod_poly_clear(y); // clear y memory
fmpz_clear(n); // clear modulus memory
}
Internally, each fmpz_mod_poly_struct
consists of several coefficients, each of which is an fmpz
integer. On creation and with manipulation, these might be allocated and changed. On deletion, each of these need to be cleared as well, which is why simply calling delete
on *fmpz_mod_poly
won't work. Instead, that will cause memory leaks.