【问题标题】:Runtime error on realloc() | inorder traversalrealloc() 上的运行时错误 |中序遍历
【发布时间】:2017-10-21 01:53:46
【问题描述】:

我正在尝试实现一个中序遍历,它返回一个包含遍历值的数组。在我的递归方法中,我尝试使用realloc() 函数来修改数组的大小并存储结果。但是,我收到以下错误:

realloc(): invalid next size

以下是我的代码:

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

void inorder(struct TreeNode *root, int *res, int *returnSize)
{
    if(root == NULL)
        return;

    //if left node present, traverse left
    inorder(root->left,res,returnSize);

    // add node to array
    res[(*returnSize)]=root->val;
    (*returnSize)++;
    int *temp = realloc(res,sizeof(int)*(*returnSize)); 
    res = temp;

    //if right node present, traverse right
    inorder(root->right,res,returnSize);
}

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* inorderTraversal(struct TreeNode* root, int* returnSize) 
{
    //check if root == null
    if(root == NULL)
    {
        return root;
    }

    //malloc result array to return
    int *res = (int *)malloc(sizeof(int)*(*returnSize));

    //start inorder parsing
    inorder(root, res, returnSize);

    return res;
}

【问题讨论】:

  • 一方面你没有检查realloc()的返回值。在调用这个之前,*returnSize 是什么?
  • 见:Do I cast the result of malloc?。同样int *temp = realloc (res, sizeof *temp * return Size); if (!temp) return res; res = temp; 将验证realloc 的返回,并防止在realloc 失败时丢失指向res 的指针。
  • 请注意,inorderTraversal 中的 res 不会在 inorder 中更新,因为按值调用。

标签: c runtime-error binary-tree inorder


【解决方案1】:

有多个问题:

  • res 的重新分配值不会传回给调用者。您应该传递一个指向res 的指针而不是它的值,或者返回新分配的指针。
  • returnSize 是一个输出变量,你应该将它初始化为1,或者更好的初始化为0,并在存储节点值之前重新分配数组。
  • 您应该处理潜在的内存分配失败。

这是一个更正的版本:

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

int *inorder(struct TreeNode *root, int *res, int *returnSize) {
    if (root != NULL) {
        //traverse the left tree
        res = inorder(root->left, res, returnSize);

        if (returnSize >= 0) {
            // add node to array
            int *temp = realloc(res, sizeof(int) * (*returnSize) + 1); 
            if (temp == NULL) {
                free(res);
                *returnSize = -1;
                res = NULL;
            } else {
                res = temp;
                res[(*returnSize)++] = root->val;

                //traverse the right tree
                res = inorder(root->right, res, returnSize);
            }
        }
    }
    return res;
}

/**
 * Return an array of size *returnSize.
 * Return NULL and *returnSize=0 for an empty tree.
 * Return NULL and *returnSize<0 for memory allocation failure.
 * Note: The returned array is malloced, the caller must call free().
 */
int *inorderTraversal(struct TreeNode *root, int *returnSize) {
    int *res = NULL;

    *returnSize = 0;
    return inorder(root, res, returnSize);
}

【讨论】:

    【解决方案2】:

    几乎可以肯定,您的代码中的其他地方存在内存损坏——这段代码对我来说看起来不错(好吧,除了不测试 realloc() 的返回是否为 NULL,但这只会导致您丢失数据,而不是获取您看到的错误)。如果您可以在您的程序上运行 valgrind,它可能会指出问题所在。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2021-08-09
    • 1970-01-01
    • 2015-01-21
    • 2013-07-29
    相关资源
    最近更新 更多