Pregunta

I want to dynamically allocate a mpz_class array.

mpz_class *arr = new mpz_class[10];

This line gives me error:

for(mpz_class i=0; i<10; i++) arr[i]=0;

It says that i can't have a mpz_class inside arr[]. Why? What if i want to allocate a really big array? Do I have to use i.get_ui()?

¿Fue útil?

Solución

To use an arbitrary precision integer as an array index is useless because memory location pointers are limited to the machine's amount of memory and it's CPU's highest integer standard, which today those are 32 or 64 bit.

Classes can not be used as a array index.

If you have to use the value stored in a mpz_class as an array index, then just use mpz_class::get_ui(); to return the value as an unsigned int.

Example:

mpz_class size = 10;
mpz_class *arr = new mpz_class[size.get_ui()];
for(mpz_class i=0; i<size.get_ui(); i++) arr[i.get_ui()] = 0;
delete[size.get_ui()] arr;

Otros consejos

for(mpz_class i=0; i<10; i++) arr[i]=0;
  // ^^^^^^^^ int ?

Didn't you mean int type for the iteration variable i ?

Also, why are you doing arr[i] = 0; ? This will loose the memory location it was earlier pointing to acquired through new[] and causes the memory leak.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top