本文介绍了c为什么重新分配不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

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是一个指针数组,递归的每一步都是强制重新分配以增加prices的大小.我遇到了许多错误错误,并且感觉到它与分配有关,我打印了sizeof(prices),发现它保持为4并且没有增加有人可以告诉我我哪里出问题了吗?

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 increaseCan someone please tell me where I went wrong?

预先感谢

P.S
编辑
我还有另一个功能可以处理**价格

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

这是当 prices = realloc(prices,sizeof(prices)* 4);

但是当我将行更改为此 prices = realloc(prices,sizeof(prices)* 150); 时,一切顺利,没有错误,因为我知道示例中的大小不会传递130,但我需要动态增加,以防不同示例的大小超过150.

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.

推荐答案

我想在代码编写和更正(编译器)错误期间,代码演变成错误的方向.

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

我觉得您实际上并不希望处理指向整数的指针数组,而是要处理(动态增长的)整数值数组(而不是指向它们的指针).然而,这种情况是函数必须重写指向数组的指针,这导致在接口中引入更多的"*",这使您陷入了困境,并且声明了 prices [* counter] =(int *)malloc(sizeof(int))向我表明这是基本的误解.

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.

让我在下面的简短示例中解释我的意思.假设我们想要一个函数 dynamicPriceListAlloc ,该函数为 nrOfItems 整数分配一个整数数组.

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.

让我们从调用者开始,即函数main:其中,由于我们要有一个动态分配的整数数组,因此我们将保存一个 int * 类型的变量,即指向该数组的指针.因为我们要在函数中分配数组,所以我们必须将指针传递给该指针,因为否则函数无法将新分配的内存地址分配给该指针.因此, dynamicPriceListAlloc 必须采用指向整数的指针,即 int ** .

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 **.

但是-现在引起误解的是-dynamicPriceListAlloc的目的不是为整数分配 10个指针的指针,而是分配一个 10个整数的数组并分配该内存块指向作为参数传递(通过引用)的指针:

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
}

我想您错过了纠正 * prices [i] = i 中的取消引用优先级的事情,而不是将其纠正为(* prices)[i] = i ,您可以通过实际为您取消引用的指针分配存储空间来解决"该问题.这就是我所说的代码朝错误的方向发展"的意思.

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

然后 printPricesArray 将进行如下修改:

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

这篇关于c为什么重新分配不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:45