Strange parallel loop behaviour int v long int <p>I have some code i'm running in parallel using an OMP for loop, up to large numbers. The code (with unimportant bits removed) is as follows (apologies if it's a mess/hard to follow):</p> <pre><code>long int primePos=-1; unsigned long long int odd; int primeFactor; while(fileNumber&lt;=nFiles) { percPrev=0; compCounter=0; #pragma omp parallel for shared(numberElements, iStart, iEnd,tau, wall0) schedule (guided) private(primePos,odd,primeFactor) for(unsigned long long int i=iStart;i&lt;=iEnd;i++){ #pragma omp atomic tau++; odd=2*i-1; if(!IsPrime(odd)){ primeFactor = getPrimeFactor(odd); for(long int j=0;j&lt;PrimeDatL;j++){ if(PrimeDataT[j]==primeFactor){ primePos=j; break; } } #pragma omp critical { if(compCounter+1&gt;numberElements){ cout&lt;&lt;"Array overflow, exiting."&lt;&lt;endl; exit(0); } InsertElement(odd,primeFactor,pHVector,primePos); compCounter++; } } } iStart=iEnd+1; iEnd=min(iEnd+numberElements,maxVal); fileNumber++; } </code></pre> <p>The idea is that I go through all the odds up to <code>2^n</code>, if they're not prime I take the smallest prime factor and put it, along with the odd, in a vector which is arranged in a particular fashion. The variable <code>primePos</code> simply denotes which spot in this vector it goes into, and isn't a particularly large number so it was originally simply an <code>int</code> type. </p> <p>However, for larger values of n ~33, I found that InsertElement() was throwing an exception because <code>primePos</code> was huge, much larger than it could possibly be as defined in the for loop which searches for the position of <code>primeFactor</code> in <code>PrimeDataT</code>. This was rectified (well, it compiles and runs and for low values of n i can verify in mathematica that it's correct) by changing <code>primePos</code> from <code>int</code> to <code>long int</code>, but i've no idea why that should be the case and I'd like to know before I continue, in case I'm missing something important.</p> <p>tl;dr = how can primePos take a value larger than PrimeDatL if it is declared as an <code>int</code> instead of <code>long int</code>? Thanks in advance!</p>