c why realloc isn't working

≯℡__Kan透↙ 提交于 2021-01-28 08:10:43

问题


I have the following code

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);

}

prices is array of pointers and every step in recursion I'm casting realloc to increase the prices size. I got many error bugs and I had a feeling it related to allocation, I printed the sizeof(prices) and I saw that it stayed 4 and not increase Can someone please tell me where I went wrong?

thanks in advance

P.S
Edit
I have another function that princt the **prices

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

This is the error I'm getting when prices = realloc(prices, sizeof(prices) *4);

but when I'm changing the line to this prices = realloc(prices, sizeof(prices) * 150); everything goes well without errors because I know that in my example size isn't going passed 130, but I need dynamic increasing incase in different example size will be over 150.


回答1:


I suppose that during code writing and correcting (compiler) errors the code evolved into a wrong direction.

I feel that you actually do not want to handle an array of pointers to integers but a (dynamically growing) array of integer values (not pointers to them). Yet the circumstance, that the function has to rewrite the pointer to the array, which led to introduce one more '*' in the interface, took you into the dilemma, and statement prices[*counter] = (int *)malloc(sizeof(int)) indicates to me that this is the basic misunderstanding.

Let me explain what I mean it on the following short example. Suppose that we want to have a function dynamicPriceListAlloc, which allocates an array of integers for nrOfItems integers.

Let's start with the caller, i.e. function main: Therein, as we want to have a dynamically allocated integer array, we will hold a variable of type int *, i.e. a pointer to this array. As we want to have the array allocated in a function, we have to pass a pointer to this pointer, because otherwise the function could not assign the newly allocated memory address to this pointer. Hence, dynamicPriceListAlloc must take a pointer to a pointer to ints, i.e. int **.

But - now the misleading thing - the intent of dynamicPriceListAlloc is not to allocate a pointer with 10 pointers to ints, but to allocate an array of 10 integers and assigning this memory block to the pointer passed (by reference) as argument:

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
}

I suppose that you missed to correct the dereference-precedence thing in *prices[i] = i, and instead of correcting this to (*prices)[i] = i, you "solved" the problem by actually allocating storage for the pointer you dereference. And that's what I meant with "the code evolved in the wrong direction".

If I am right with this assumption, then your code would change as follows:

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);
}

And printPricesArray would be adapted as follows:

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


来源:https://stackoverflow.com/questions/41750219/c-why-realloc-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!