Warm tip: This article is reproduced from serverfault.com, please click

Accessing MultiDim Array gives Seg Fault C++

发布于 2020-12-03 14:24:45

I am doing a practise problem in which I have to find Number of K-primes(Numbers having K distinct prime factors) between two given integers. I have coded the solution however I am getting a segmentation fault, I have pinpointed the location where I am getting Seg Fault

table[prime_factor_nos[i]][i]++;

I have created a global 2D array of size 6 by N(1e5 + 2). However when I reduce N to 1e4 it works as expected. For now I only know that improper indexing is not giving this issue rather memory. I have also tried printing the values while looping, the seg fault occurs at this value

cout<<"i :"<<i<<" pf[i] "<<prime_factor_nos[i]<<" "<<table[prime_factor_nos[i]][i]<<nl

Seg fault occurs at i = 30015

Last output:

i :30014 pf[i] 3 0
i :

Is this a memory issue? Is the static storage not enough for this?

Code

Questioner
Praful
Viewed
0
starboy_b 2020-12-03 22:58:42

For 30030 and some more values, it has prime_factor_nos[30030] = 6, That's why it is causing a runtime error.