2017-01-19 7 views
1

に動作していない理由を私は次のコードを持っているCのreallocは

void getPariceArray(Board board, treeNode *tn, Position *dst, int **prices, int *counter, int total) 
{ 

    if (tn == NULL) 
     return NULL; 
    if (tn->position[0] == dst[0][0] && tn->position[1] == dst[0][1]) 
    { 
     prices = (int **)realloc(prices, sizeof(prices) *4); 
     prices[*counter] = (int *)malloc(sizeof(int)); 
     printf("%d", sizeof(prices)); 
     *prices[*counter] = total; 
     *counter = *counter + 1; 
    } 

    int x = tn->position[1] - '1'; 
    int y = tn->position[0] - 'A'; 

    int cellPrice = board[x][y] - '0'; 

    total += cellPrice; 

    getPariceArray(board, tn->up, dst, prices, counter, total); 
    getPariceArray(board, tn->down, dst, prices, counter, total); 
    getPariceArray(board, tn->right, dst, prices, counter, total); 
    getPariceArray(board, tn->left, dst, prices, counter, total); 

} 

価格はポインタの配列と私は価格のサイズを大きくするのreallocをキャストしています再帰内のすべてのステップです。 多くのエラーのバグがあり、割り振りに関連する感じがありました 私はsizeof(価格)を印刷しましたが、それは4にとどまり、増えないことがわかりました 誰かが間違っていた場所を教えてください。事前に

おかげ

PS
編集
私は**価格をprinct別の機能を持っている

void printPricesArray(int **arr, int length) 
{ 
    for (int i = 0; i < length; i++) 
    { 
     printf("place:%d Price:%d\n", i, *arr[i]); 
    } 
} 

これは私が得ているエラーのときprices = realloc(prices, sizeof(prices) *4);

error bug しかし、私がこのラインを変更するときには、prices = realloc(prices, sizeof(prices) * 150);すべてがうまくいかない私の例ではサイズが130になっていないことが分かっているからですが、サイズが違うとサイズが大きくなる必要があります。 no errors bug

+1

'のrealloc(価格、はsizeof(価格) * 4); '文が再度実行されたときには何も変わらない。メモリの割り当ては、そのサイズにとどまります:4つの配列要素。 'sizeof(prices)'であっても、ポインターのサイズであり、ポインターのデータサイズではありません。 –

+0

'私はsizeof(価格)'を印刷しました。 'prices'はポインタです。 'sizeof'ポインタは常に同じものを返します。また、Cで 'malloc'や' realloc'をキャストする必要はありません。 – DeiDei

+0

これを増やす方法はありますか? – Brec

答えて

1

私は、コードの書き込みや修正コードが間違った方向に進化しました。

私は実際には整数へのポインタの配列を扱いたくないと思いますが、(動的に増加する)整数値の配列(それらへのポインタではありません)。しかし、この関数は配列へのポインタを書き換える必要があり、インタフェースにもう1つの '*'を導入し、ジレンマに陥り、ステートメントprices[*counter] = (int *)malloc(sizeof(int))はこれが基本的な誤解であることを示しています。

私は次の短い例でそれを意味するものを説明しましょう。 nrOfItems整数に整数の配列を割り当てる関数dynamicPriceListAllocがあるとします。

呼び出し元、つまり関数mainから始めましょう:ここでは、動的に割り当てられた整数配列を取得するために、int *という変数、つまりこの配列へのポインタを保持します。関数内で配列を割り当てたいので、関数がこのポインタに新しく割り当てられたメモリアドレスを割り当てることができないので、このポインタへのポインタを渡さなければなりません。したがって、dynamicPriceListAllocは、intへのポインタへのポインタ、すなわちint **を取る必要があります。

しかし - 今誤解を招くもの - dynamicPriceListAllocの目的は10ポインタint型へでポインタを割り当てることではなく、で(10整数と渡されたポインタにこのメモリブロックを割り当てるの配列を割り当てます引数として参照):

int main(){ 

    int *priceList; 
    dynamicPriceListAlloc(&priceList, 10); 

    for (int i=0; i<10; i++) 
     printf("%d\n", priceList[i]); 
} 

void dynamicPriceListAlloc(int **prices, int nrOfItems) { 
    *prices = (int*)malloc(nrOfItems * sizeof(int)); 

    for (int i=0; i<nrOfItems; i++) 
     // *prices[i] = i; // Wrong: takes prices[i] and then dereferences it 
     (*prices)[i] = i; // OK: derefernces prices (yielding a pointer an int-array) and then setting the i'th element 
} 

私はあなたが*prices[i] = iに間接参照優先順位のものを修正するために逃し、代わり(*prices)[i] = iにこれを修正するの、あなたが実際にポインタあなたの間接参照用のストレージを割り当てることによって、問題を「解決」したとします。そして、それは私が「コードが間違った方向に進化した」という意味です。

私は右のこの仮定にしています場合は、次のようにコードが変更になります。次のように

void getPariceArray(Board board, treeNode *tn, Position *dst, int **prices, int *counter, int total) 
{ 
    if (tn == NULL) 
     return; 
    if (tn->position[0] == dst[0][0] && tn->position[1] == dst[0][1]) 
    { 
     size_t sizeOfPrices = (*counter) * sizeof(int); 
     *prices = (int*)realloc(*prices, sizeOfPrices); 
     printf("size of prices: %ld", sizeOfPrices); 
     (*prices)[*counter] = total; 
     *counter = *counter + 1; 
    } 

    int x = tn->position[1] - '1'; 
    int y = tn->position[0] - 'A'; 

    int cellPrice = board[x][y] - '0'; 

    total += cellPrice; 

    getPariceArray(board, tn->up, dst, prices, counter, total); 
    getPariceArray(board, tn->down, dst, prices, counter, total); 
    getPariceArray(board, tn->right, dst, prices, counter, total); 
    getPariceArray(board, tn->left, dst, prices, counter, total); 
} 

そしてprintPricesArrayが適応されます:

void printPricesArray(int *arr, int length) 
{ 
    for (int i = 0; i < length; i++) 
    { 
     printf("place:%d Price:%d\n", i, arr[i]); 
    } 
} 
+0

'int ** prices'は私の決定ではありませんでしたが、私の学校1、その学校のプロジェクトと彼らは私に機能の定義を与えました。しかし私は同意する、int arrayははるかに簡単だった。 – Brec

+0

2回目の読書後、試してみましたが実際に動作します – Brec

関連する問題