0

in this case occur "segmentation fault"

#define MAX_NODE 200001
#define MAX_DP 19

using namespace std;

int main()
{
    int next[MAX_NODE][MAX_DP];
    return 0;
}

But, this case do not occur

#define MAX_NODE 200001
#define MAX_DP 19

using namespace std;

int main()
{
    int **next = (int **)malloc(MAX_NODE * sizeof(int *));
    for (int i = 0; i < MAX_NODE; i++)
    {
        next[i] = (int *)malloc(MAX_DP * sizeof(int));
    }
    return 0;
}

what is one make this situation?

g++ version: Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin20.3.0 Thread model: posix

chichchic
  • 3
  • 1

1 Answers1

1

You are overflowing your stack. Stack space is limited, which is why large objects should be allocated on the heap (new).

int next[MAX_NODE][MAX_DP]; creates over 10 MB of stack allocations, which is certainly larger than the average stack size.

Stewart Smith
  • 1,396
  • 13
  • 28
  • Thanks for the answer. But, I am wondering if there is also a way to change the 10MB limit. – chichchic May 17 '21 at 13:18
  • See https://stackoverflow.com/questions/2279052/increase-stack-size-in-linux-with-setrlimit. There is probably something similar in windows. – Stewart Smith May 17 '21 at 16:29