Segmentation fault on properly allocated array : <p>I get a segmentation fault while assigning values to what looks to me a properly allocated array. Here is the code below:</p>

<pre><code>int SpV = L*L*L;
int V = SpV*T;
int M = 16;
int Ns = 2;
int Np = 10;

float *twopBuf = (float*) malloc(Np*Ns*V*M*2*sizeof(float));
if(twopBuf == NULL){
  fprintf(stderr,"Cannot allocate twopBuf. Exiting\n");
  exit(-1);
}

for(int bar=0;bar&lt;Np;bar++)
  for(int pr=0;pr&lt;Ns;pr++)
    for(int t=0;t&lt;T;t++)
      for(int v=0;v&lt;SpV;v++)
        for(int gm=0;gm&lt;M;gm++){
          int pos = 2*gm + 2*M*v + 2*M*SpV*t + 2*M*SpV*T*pr + 2*M*SpV*T*Ns*bar;

          twopBuf[ 0 + pos ] = 1.0; // Set to 1.0 for
          twopBuf[ 1 + pos ] = 1.0; // testing purposes
        }
</code></pre>

<p>L and T are input, so when say <code>L = 32</code> and <code>T = 64</code> it runs fine. But for <code>L = 48</code> and <code>T = 96</code> I get segmentation fault after <code>bar</code> becomes 2 and before it becomes 3. If there wasn't enough memory to allocate <code>twopBuf</code> wouldn't I already get the error message?</p>

<p>I'm running this on the head node of a large supercomputer, if it makes a difference. Thanks.</p>
