2011-12-02 147 views
3
#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; 
    } 

我開始磅=(INT **)...線大約堆是這樣得到錯誤:內存分配問題

「Windows已經引發了TestProj.exe一個斷點。

這可能是由於堆,這表明在TestProj.exe或任何它已加載的DLL的一個錯誤的損壞。

這也可能是由於用戶按F12而TestProj.exe有重點。

輸出窗口可能有更多診斷信息。「

+0

你打F12了嗎? – Pubby

+0

你可以把你的問題歸結爲一個更簡短的例子嗎? – arne

回答

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

我想你應該有sizeof(double)不是指針,因爲它是一個double值的數組。

1

按如下方式分配內存可以避免在整個地方重複使用類型名稱,因此您不會錯配它。

double* pi = calloc(MAXSTATES, sizeof(*pi)); 
0
int **psi , i; 

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

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

嘗試這樣做,和一個小檢查,如果PSI不爲空可能是巨大的^^。