Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an integer 2d matrix NUMI and a 3d double matrix called PROB.

Here the 2 allocation:

   int main ( int argc, char* argv[]){
        double  ***PROB;
        int  **NUMI;    
        NUMI = (int **)malloc((dim)*sizeof(int *));
        PROB = (double ***)malloc((dim)*sizeof(double**));
        ...    
        for( i = 0; i < n ; i++){
            PROB[ACT][ACTSTART][i] = value;
            NUMI[i][i]= value2;
        }


}
  • How many rows and cols has NUMI? Is a dim x dim matrix???

  • PROB is a 3d matrix...here the allocation is dim x dim x dim?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should allocate memory for every dimension:

NUMI = (int **)malloc((dim)*sizeof(int *));
for (i = 0; i < dim; i++)
    NUMI[i] = (int*)malloc(dim * sizeof(int));

PROB = (double ***)malloc((dim)*sizeof(double**));
for (i = 0; i < dim; i++)
{
    int j;
    PROB[i] = (double**)malloc(dim* sizeof(double*));
    for (j = 0; j < dim; j++)
    {
        PROB[i][j] = (double*)malloc(dim * sizeof(double));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...