0

I'm working on a project that uses the FLINT library, and I'm running into some problems defining a large fmpz array. Here's a minimal example of the problem

#include <iostream>
#include <fmpz_mod_poly.h>
#include <fmpz_mod.h>
#include<cstdlib>
#include<array>
using namespace std;
int main()
{
    //Here k is 2^20, I just used a bit shift trick to calculate it quickly
    //This does not run, but for int k  = 1<<19 it does run.
    int k = 1<<20;
    fmpz TestArray[k];
    for(int i = 1; i<=k;i++)
    {
        TestArray[i] = i;
    }
return 0;

and the error code is Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ff7bf6ffff8).

If I replace the line

int k = 1 << 20;

with the line

int k = 1<<19;

then the program compiles with no error. I'm not sure what the issue is. I was under the impression that there was no maximum array size in C++.

  • 1
    Doesn't have anything to do with flint. The stack has a fixed size depending on your platform and compile flags. If you exceed it you have a problem. – Retired Ninja Sep 15 '22 at 03:38
  • 1
    That is not too big for an array in C++, presuming you have the memory for it.; but you are creating it as a local variable, which normally means on the stack and It is almost certainly too big for that. You are also trying to use a VariableLengthArray, which is not standard C++. Why don't you use a std::vector instead. It is standard C++, runtime resizable, the body of the array is dynamically allocated, and you can get a pointer to the start of the array for use in library interfaces. – Avi Berger Sep 15 '22 at 03:39
  • I see, yes these all seem to work. Thank you so much. Is there a way I can mark a question as solved when an answer hasn't been posted? – MathManiac5772 Sep 15 '22 at 03:48

0 Answers0