Code::Blocks crashes :Process returned (0xC0000005)

0

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.

#include <stdio.h>
#include "string.h"
#include <math.h>
#include <stdlib.h>
int BinarySearch(double probrnd, double *prob_cumsum, int Ksize) {
int k, kstart, kend;
if (probrnd <=prob_cumsum[0])
    return(0);
else {
    for (kstart=1, kend=Ksize-1; ; ) {
        if (kstart >= kend) {
            return(kend);
        }
        else {
            k = kstart+ (kend-kstart)/2;
            if (prob_cumsum[k-1]>probrnd && prob_cumsum[k]>probrnd)
                kend = k-1;
            else if (prob_cumsum[k-1]<probrnd && prob_cumsum[k]<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<WordNum[0];i++){
    j = (int) SampleDex[i] -1;
    k = (int) z[j] -1;
    if(z[j]>0){
        n_k[k]--;
    }
    for (cum_sum=0, k=0; k<Ksize[0]; k++) {
        cum_sum += n_k[k]-a[0];
        prob_cumsum[k] = cum_sum;
    }

    if ( ((double) rand() / RAND_MAX * (cum_sum + mass) < cum_sum)){
        probrnd = (double)rand()/(double)RAND_MAX*cum_sum;
        k = BinarySearch(probrnd, prob_cumsum, Ksize[0]);
    }
    else{
        for (k=0; k<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;}
c
crash
codeblocks
asked on Stack Overflow Jan 7, 2016 by Siamak

1 Answer

0

Let's reduce your problem to a minimal example that exhibits the same behaviour:

int main()
{
    double *z;

    z[0] = 1;       // Undefined behaviour!

    return 0;
}

Here, z is an uninitialized pointer; it points to a random location. De-referencing it, that is trying to read or write to where the pointer points to leads to undefined behavior.

You use z as an array of 5 floating-point numbers. The easiest way to fix this is to create z as a local array:

double z[5] = {1, 1, 2, 0, 0};

There are probably more errors in your code. This answer addresses only the cause of the obvious segmentation violation.

answered on Stack Overflow Jan 7, 2016 by M Oehm • edited Mar 28, 2019 by rv7

User contributions licensed under CC BY-SA 3.0