Junaid
Junaid

Reputation: 1678

Memory allocation issue with multidimensional array

#define MAXROWS 88
#define MAXSTATES 10
#define MAXPROBS 6

int obs[MAXROWS]= {0,5,2,3,0,5,2,3,2,4,0,3,5,1,4,3,1,5,2,0,4,4,1,5,3,3,1,4,0,5,1,2,3,0,2,0,5,2,0, 4,4,5,3,0,5,2,5,1,5,4,0,3,1,4,5,2,3,5,1,5,2,4,5,1,5,4,2,5,0,3,4,1,5,2,4,1,5,0,4,2,3,0,5,1,5,2,4,1};//{2,1,0} ;
int q[MAXROWS]= {1};
int s=MAXROWS, i=1,j=0;
double **A; 
double **B; 
double AD[MAXSTATES][MAXSTATES]={{0,1,0,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0},{0,0,0,1,0,0,0,0,0,0},{0,0,0,0,1,0,0,0,0,0},{0.8,0,0,0,0,0.2,0,0,0,0},{0,0,0,0,0,0,1,0,0,0},{0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0,1,0},{0.2,0,0,0,0,0.8,0,0,0,0}};//{{.6,.4},{.3,.7}};//{  { .500, .375, .125 }, { .250,.125, .625 }, { .250,.375,.375 } };
double BD[MAXSTATES][MAXPROBS]={{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15}};//{{.1,.3,.6},{.5,.4,.1}};//{ { .60, .20 ,.15, .05}, { .25, .25, .25, .25 }, { .05,.10,.35,.50 } };
double *pi;
double pi2[MAXSTATES] =  {1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//{.4,.6};
double *poolA;
double *curPtrA;
double *poolB;
double *curPtrB;
double pproba=0;
double **delta;
double *pool;
double *curPtr;

int **psi;
int *poolpsi;
int *curPtrpsi;

HMM model; 

A = (double** )calloc(MAXSTATES, sizeof(double* ));

poolA = (double *)calloc( MAXSTATES * MAXSTATES, sizeof(double));
curPtrA = poolA;

for( i = 0; i < MAXSTATES; i++)
{
    *(A + i) = curPtrA;
        curPtrA += MAXSTATES;
}

B = (double** )calloc(MAXSTATES, sizeof(double* ));

poolB = (double *)calloc( MAXSTATES * MAXPROBS, sizeof(double));
curPtrB = poolB;

for( i = 0; i < MAXSTATES; i++)
{
    *(B + i) = curPtrB;
        curPtrB += MAXPROBS;
}

for(i = 0; i <MAXSTATES; i++)
    for(j=0; j< MAXPROBS; j++)
        B[i][j] = BD[i][j];

for(i = 0; i < MAXSTATES; i++)
    for(j=0; j< MAXSTATES; j++)
        A[i][j] = AD[i][j];


pi = (double* )calloc(MAXSTATES, sizeof(double* ));

for(i = 0; i <MAXSTATES; i++)
    pi[i] = pi2[i];


model.M=MAXPROBS;
model.N=MAXSTATES;
model.A= A; 
model.B = B;
model.pi = pi; 
//double delta[6][4];


    psi = (int** )calloc(MAXROWS, sizeof(int* ));

    poolpsi = (int *)calloc( MAXROWS*MAXSTATES, sizeof(int));
    curPtrpsi = poolpsi;

    for( i = 0; i < MAXROWS; i++)
    {
        *(psi + i) = curPtrpsi;
         curPtrpsi += MAXSTATES;
    }

I start getting error on psi = (int **) ... line about heaps which is like this:

"Windows has triggered a breakpoint in TestProj.exe.

This may be due to a corruption of the heap, which indicates a bug in TestProj.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while TestProj.exe has focus.

The output window may have more diagnostic information."

Upvotes: 3

Views: 138

Answers (3)

M07
M07

Reputation: 1131

   int **psi , i;

    psi = malloc( MAXROWS * sizeof(int*));

    for( i = 0 ; i < MAXROWS ; i++ )
    {
         psi[i] = calloc (MAXSTATES, sizeof(int));
    }

Try to do that and a little check if psi is not null could be great ^^.

Upvotes: 0

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234484

Allocating memory as follows avoids the need to repeat the name of the type all over the place, and thus you won't mismatch it.

double* pi = calloc(MAXSTATES, sizeof(*pi));

Upvotes: 1

AndersK
AndersK

Reputation: 36082

pi = (double* )calloc(MAXSTATES, sizeof(double* ));

i think you should have sizeof(double) not pointer since its an array of double values.

Upvotes: 1

Related Questions