簡體   English   中英

在main外部定義的函數中分配main中的數組

[英]Allocate array in main inside a function which is defined outside of main

我正在嘗試在函數內部的數組上使用calloc,但是它不起作用。 嘗試調試時,我發現該函數內部的指針指向分配的內存,但是當離開該函數時,指針再次指向NULL。 嘗試了各種變體,但似乎找不到解決方案。

這是我的代碼:

int main(int argc, char *argv[]) {
    int *rows = NULL, *solvedRows = NULL;
    int **board = NULL, **solvedBoard = NULL;
    allocateMemory(dim, &(*rows), &(*board));
    allocateMemory(dim, &(*solvedRows), &(*solvedBoard));
}

void allocateMemory(int dim, int** rows, int*** board) {
    rows = calloc(dim*dim,sizeof(int));
    board = calloc(dim, sizeof(int*));
    if (rows == NULL || board == NULL) {
        printf("Error: calloc has failed\n");
        exit(1);
    }
}

需要幫助以了解問題所在以及如何解決。

編輯

我試過了:

*rows = calloc(dim*dim,sizeof(int));
*board = calloc(dim, sizeof(int*));

仍然有同樣的問題。

還嘗試了:

allocateMemory(dim, &rows, &board);

對於第4行和第5行(與第5行相同),並且in不會與錯誤一起編譯:“錯誤:從不兼容的指針類型[-Werror = incompatible-pointer-types]傳遞'allocateMemory'的參數2分配內存(dim,&rows和&board ); ^“錯誤:從不兼容的指針類型[-Werror = incompatible-pointer-types]傳遞'allocateMemory'的參數3; allocateMemory(dim,&rows,&board); ^

編輯

對於任何遇到此問題並檢查此頁面的人,最后一次嘗試實際上是正確的,正如邁克爾在下面回答。 該錯誤是相應頭文件中的錯誤,並且在修復頭文件時已得到修復。

讓我們關注您的rows變量:

rows是一個指針,它是一個保存內存地址的變量。 現在,您希望alocateMemory將一個內存塊的地址寫入rows ,以保存您的數據。 很簡單:

size_d dim = 10;
int* rows = 0;
rows = calloc(1, sizeof(int) * dim);

但是,如果您將此代碼放入類似

void allocateMemory(size_t dim, int* rows) {
    rows = calloc(1, sizeof(int) * dim);
}

現在,如果您像這樣調用此函數

int* actualRows = 0;
allocateMemory(3, actualRows);

actualRows (即0 )將被復制到一個新的變量rows ,您可以在allocateMemory操作。 當您寫入行時,其值會更改,但是當剩下allocateMemory時, rows被破壞。 決不會actualRows改變。

您想要的是allocateMemory設置內存行中的actualRows的值。 為此,您必須為allocateMemory提供與actualRows類似的地址

allocateMemory(3, &actualRows);

&atualRows是存儲器地址actualRows和其類型是int** (指針的指針來int )。

現在,您必須適當地修改allocateMemory的簽名:

void allocateMemory(size_t dim, int** rows) {
    rows = calloc(1, sizeof(int) * dim);
}

而且,由於行現在是一個指針,並且您想更改其目標,因此需要在分配它之前取消引用它:

*rows = calloc(1, sizeof(int) * dim);

總而言之:

void allocateMemory(size_t dim, int** rows) {
    *rows = calloc(1, sizeof(int) * dim);
}

...
int* actualRows = 0;
allocateMemory(3, &actualRows);
...

對於您的board ,原則上相同。 嘗試

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

void allocateMemory(int dim, int** rows, int*** board) {
    *rows = calloc(dim * dim,sizeof(int));
    *board = calloc(dim, dim * sizeof(int*));

    if (rows == NULL || board == NULL) {
        printf("Error: calloc has failed\n");
    }

    for(size_t i = 0; i < dim; ++i) {
        (*board)[i] = calloc(1, sizeof(int) * dim);

        if ((*board)[i] == NULL) {
            printf("Error: calloc has failed\n");
        }
    }

}

int main(int argc, char *argv[]) {

    size_t dim = 10;

    int *rows = NULL;
    int **board = NULL;
    allocateMemory(dim, &rows, &board);

    rows[0] = 10;
    rows[1] = 11;

    for(size_t i = 0; i < dim; ++i) {
        printf("%i ", rows[i]);
    }

    printf("\n\n\n");

    board[0][0] = 11;
    board[9][0] = 99;
    board[9][9] = 101;

    for(size_t i = 0; i < dim; ++i) {

        for(size_t k = 0; k < dim; ++k) {

            printf("%i ", board[i][k]);
        }
        printf("\n");
    }

    printf("\n");

    for(size_t i = 0; i < dim; ++i) free(board[i]);
    free(board);

    free(rows);
}

暫無
暫無

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

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