簡體   English   中英

在C中分配2D數組

[英]Mallocing a 2d array in C

我一直在嘗試使用C中的雙指針創建一個2d整數數組,並創建了以下代碼。 但是,我不明白為什么語句a [1] [1]會導致段錯誤。 此時我的代碼行的值為3,cols也是如此。 我很肯定這是段錯誤來自的代碼部分。 有誰知道如何使用雙指針和沒有像int [5] [2]這樣的聲明正確分配2d數組。

int **a;
int **b;
int **c;
a = malloc(rows * sizeof(int *));
b = malloc(rows * sizeof(int *));
c = malloc(rows * sizeof(int *));

for (i = 0; i < rows; i++)
{
    a[i] = (int*) malloc(cols * sizeof(int));
}

for (i = 0; i < rows; i++)
{
    b[i] = (int*) malloc(cols * sizeof(int));
}

for (i = 0; i < rows; i++)
{
    c[i] = (int*) malloc(cols * sizeof(int));
}

a[1][1] = 5;

考慮以下示例(其中allocIntArrayfreeIntArray函數可用於不同大小的D2數組):

#include <stdlib.h>
#include <stdio.h>

int ** allocIntArray(int nrows, int ncols)
// allocate memory for 2D array and returns pointer if successful or NULL if failed
{
    int r, c; // rows and cols counters
    int ** parray; // pointer to array
    // allocate memory for rows pointers
    parray = (int **) malloc(nrows * sizeof(int *));
    if( parray == NULL) // check
    {
        return NULL; // error sign
    }
    // allocate memory for each row
    for (r = 0; r < nrows; r++)
    {
        parray[r] = (int*) malloc(ncols * sizeof(int));
        if( parray[r] == NULL ) // check
        {
            // clean memory that was allocated before error
            while(--r >= 0)
            {
                free(parray[r]);
            }
            free(parray);
            return NULL; // error sign
        }
    }
    // return when success 
    return parray;
}

int freeIntArray(int nrows, int **parray)
// frees memory allocated for 2D array and returns 1 if successful or 0 if failed
{
    int r; // rows counter
    if( parray == NULL || nrows < 1)
    {
        return 0;
    }
    // free memory allocated for each row
    for (r = 0; r < nrows; r++)
    {
        if(parray[r] != NULL)
        {
            free(parray[r]);
        }
    }
    // free memory allocated for rows pointers
    free(parray);
    return 1;
}

int main()
{
    int ** myarray = allocIntArray(5, 6); // pointer to array

    // check that array allocated
    if(myarray == NULL)
    {
        printf("cannot allocat memory\n");
        return 1;
    }
    // work with array examples
    int c, r;
    // write values using pointer arithmetic
    for (r = 0; r < 5; r++)
    {
        for(c = 0; c < 6; c++)
        {
            *(*(myarray+r) + c) = r * 10 + c;
        }
    }
    // read values using usual 2-steps-indexing
    for (r = 0; r < 5; r++)
    {
        for(c = 0; c < 6; c++)
        {
            printf("%4d", myarray[r][c] );
        }
        printf("\n");
    }
    // free memory
    freeIntArray(5, myarray);
    return 0;
}

您的解決方案看起來非常復雜。 我更喜歡以下內容來分配2D數組:

    int nrows = 5; // number of rows
    int ncols = 10; // number of columns
    int r, c; // rows and cols counters
    int ** parray = NULL; // pointer to array

    // allocate memory for rows pointers
    parray = (int **) malloc(nrows * sizeof(int *));
    if( parray == NULL) // check
    {
        return 1; // error
    }
    // allocate memory for each row
    for (r = 0; r < nrows; r++)
    {
        parray[r] = (int*) malloc(ncols * sizeof(int));
        if( parray[r] == NULL ) // check
        {
            return 2; // error
        }
    }
    // working with array (just an example
    for (r = 0; r < nrows; r++)
    {
        for(c = 0; c < ncols; c++)
        {
            parray[r][c] = r * 10 + c;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM