Code::Blocks crashes :Process returned (0xC0000005)
<p>I am trying to build and run the following code on code blocks, but it crashes and returns the (0xC0000005) error. I mainly suspect the usage of realloc for pointer n_k in gCRSF_gibbs function, however I am not sure how to track the source of crash. This is not my original code and I am also a noob in C. Any kind of help is appreciated.</p>

<pre><code>#include &lt;stdio.h&gt;
#include "string.h"
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;
int BinarySearch(double probrnd, double *prob_cumsum, int Ksize) {
int k, kstart, kend;
if (probrnd &lt;=prob_cumsum[0])
    return(0);
else {
    for (kstart=1, kend=Ksize-1; ; ) {
        if (kstart &gt;= kend) {
            return(kend);
        }
        else {
            k = kstart+ (kend-kstart)/2;
            if (prob_cumsum[k-1]&gt;probrnd &amp;&amp; prob_cumsum[k]&gt;probrnd)
                kend = k-1;
            else if (prob_cumsum[k-1]&lt;probrnd &amp;&amp; prob_cumsum[k]&lt;probrnd)
                kstart = k+1;
            else
                return(k);
        }
    }
}
return(k);}
void gCRSF_gibbs(double *z, double *n_k, double *SampleDex,
             double *r, double *a, double *p,
             int *Ksize, int *WordNum) {

int i, j, k;
double mass;

double *prob_cumsum;
double cum_sum, probrnd;
void *newptr;

prob_cumsum = (double *) calloc(Ksize[0],sizeof(double));

mass = r[0]*pow(p[0],-a[0]);
for (i=0;i&lt;WordNum[0];i++){
    j = (int) SampleDex[i] -1;
    k = (int) z[j] -1;
    if(z[j]&gt;0){
        n_k[k]--;
    }
    for (cum_sum=0, k=0; k&lt;Ksize[0]; k++) {
        cum_sum += n_k[k]-a[0];
        prob_cumsum[k] = cum_sum;
    }

    if ( ((double) rand() / RAND_MAX * (cum_sum + mass) &lt; cum_sum)){
        probrnd = (double)rand()/(double)RAND_MAX*cum_sum;
        k = BinarySearch(probrnd, prob_cumsum, Ksize[0]);
    }
    else{
        for (k=0; k&lt;Ksize[0]; k++){
            if ((int) n_k[k]==0){
                break;
            }
        }
        if (k==Ksize[0]){
            Ksize[0]++;
            newptr = realloc(n_k,sizeof(*n_k)*Ksize[0]);
            n_k = newptr;
            n_k[Ksize[0]-1]=0;
            prob_cumsum =  realloc(prob_cumsum,sizeof(*prob_cumsum)*Ksize[0]);
        }
    }
    z[j] = k+1;
    n_k[k]++;
}
free(prob_cumsum);}

int main() {
double *z, *n_k, *sampleDex;
double *r, *a, *p;
int *Ksize, *WordNum;
z[0]=1;z[1]=1;z[2]=2;z[3]=0;z[4]=0;
n_k[0]=2;n_k[1]=1;
sampleDex[0]=4;sampleDex[1]=5;
r[0]=5;a[0]=0.5;p[0]=0.5;
Ksize[0]=2;WordNum[0]=2;
gCRSF_gibbs(z,n_k,sampleDex,r,a,p,Ksize,WordNum);
return 0;}
</code></pre>