How do I correctly set up, access, and free a multidimensional array in C?

后端 未结 5 961
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 23:31

I have seen dozens of questions about “what’s wrong with my code” regarding multidimensional arrays in C. For some reason people can’t seem to wrap their head around what is

5条回答
  •  名媛妹妹
    2020-11-22 00:06

    Statically speaking, this is easy to understand:

    int mtx[3][2] = {{1, 2},
                     {2, 3},
                     {3, 4}};
    

    Nothing complicated here. 3 rows, 2 columns; data in column one: 1, 2, 3; data in column two: 2, 3, 4. We can access the elements via the same construct:

    for(i = 0; i<3; i++){
        for(j = 0; j<2; j++)
            printf("%d ", mtx[i][j]);
        printf("\n");
    }
    //output
    //1 2
    //2 3
    //3 4
    

    Now let’s look at this in terms of Pointers:

    The brackets are a very nice construct to help simplify things, but it doesn’t help when we need to work in a dynamic environment, so we need to think of this in terms of pointers. If we want to store a “row” of integers, we need an array:

    int row[2] = {1,2};
    

    And you know what? We can access this just like a pointer.

    printf("%d, %d\n",*row,*(row+1));   //prints 1, 2
    printf("%d, %d\n",row[0],row[1]);   //prints 1, 2
    

    Now if we don’t know the number of values in a row we can make this array a dynamic length if we have a pointer to int, and we give it some memory:

    int *row = malloc(X * sizeof(int));  //allow for X number of ints
    *row = 1;        //row[0] = 1
    *(row+1) = 2; //row[1] = 2
    …
    *(row+(X-1)) = Y; // row[x-1] = Some value y
    

    So now we have a dynamic 1 dimensional array; a single row. But we want lots of rows, not just one, and we don’t know how many. That means we need another dynamic 1 dimensional array, each element of that array will be a pointer which points to a row.

    //we want enough memory to point to X number of rows
    //each value stored there is a pointer to an integer
    int ** matrix = malloc(X * sizeof(int *));
    
    //conceptually:
    (ptr to ptr to int)     (pointer to int)
       **matrix ------------> *row1 --------> [1][2]
                              *row2 --------> [2][3]
                              *row3 --------> [3][4]
    

    Now all that’s left to do is to write the code which will perform these dynamic allocations:

    int i, j, value = 0;
    
    //allocate memory for the pointers to rows
    int ** matrix = malloc(Rows * sizeof(int*));
    
    //each row needs a dynamic number of elements
    for(i=0; i

    One of the most important things to do now is to make sure we free the memory when we’re done. Each level of malloc() should have the same number of free() calls, and the calls should be in a FILO order (reverse of the malloc calls):

    for(i=0; i

提交回复
热议问题