_CrtIsValidHeapPointer(pUserData)错误:Realloc()指针

时间:2013-10-02 16:27:49

标签: c pointers realloc singly-linked-list

在VS2012(C)中编译我的代码时,我收到了_CrtIsValidHeapPointer(pUserData)消息。

我意识到我试图在指针上使用realloc,所以当我阅读其他帖子时,很多人建议使用memcpyconst char **。我真的很喜欢这样做,但我只是不知道如何。

Code的第一段片段:我用于realloc的指针的Typedef结构

int clientcounter = 0; //Global variable since this will be needed in other functions

typedef struct client{
    char name[254];
    int birthday;
    int landline;
    int cellphone;
    int clientnum;
    struct client *next; 
}clientdata;

我的程序读取文件的内容并通过Singly linked-list将其存储到这些变量中。

代码的第二部分:

void loading_file(clientdata curr[])
{
    clientdata *dummy[10000] = {0};
    clientdata *head;
    clientdata *tail;

    head = NULL;
    tail = NULL;

    //.... Asking for roster file

    //.. memory allocation of dummy[clientcounter]

    //.... Storing data to variables

    if (head == NULL)
    {
        head = dummy[clientcounter];
    }
    else
    {
        tail->next=dummy[clientcounter];
    }
    tail = dummy[clientcounter];

    if (head!=NULL)
    {
        dummy[clientcounter-1] = head;
        do {
            printf("\n ::%s:: Name\n",dummy[clientcounter]->name);
            curr[clientcounter] = dummy[clientcounter];
            dummy[clientcounter] = realloc(dummy,sizeof(item*) * clientcounter); //Error appears Here
            dummy[clientcounter] = NULL;
            clientcounter++;
            dummy[clientcounter] = dummy[clientcounter-1]->next;
        }while(dummy[totalitem] != NULL);
    }
    free(dummy[totalitem-1]);
}

有人告诉我,每次clientcounter++发生时,我都必须重新分配内存,以免泄漏。我的问题是,我知道导致错误的原因(重新分配指针),但我不知道如何修复它。

有人可以教我如何修复代码吗?

1 个答案:

答案 0 :(得分:1)

如果最终目标是简单地将数据文件加载到链表中,那么大多数发布的代码都是无关紧要的,根本不需要。以下是将数据文件加载到前向链表中的非常简单实现。

需要什么

一般算法是这样的:

  • 从输入文件中读取一行。
  • 将线条解析为我们的结构成员。
  • 如果所有成员都已正确解析,请创建一个新项目并将其添加到列表中。

最后一步是上面代码的核心,我将在代码之后解释它是如何工作的。这个修改过的函数将文件名作为唯一的输入参数进行处理,并返回clientdata项的链接列表,解析其中你还需写,所以不要认为你可以免费获得所有这些:

守则

clientdata* load_file(const char fname[])
{
    FILE *fp = fopen(fname, "r");
    char line[512]; // suitable value

    // pp always holds the *address* of the pointer where the
    //  next node will be added to the list. notice how it first
    //  holds the address of our list-head pointer.
    clientdata *result = NULL;
    clientdata **pp = &result;

    while (fgets(line, sizeof(line), fp))
    {
        clientdata val = {0};

        //
        // TODO: parse line into val-members using things like
        //  sscanf(), strtok(), or others, etc.
        //

        if (parsed-successfully-condition)
        {
            // allocate a new list node, copy over the data
            *pp = malloc(sizeof(**pp));
            if (*pp)
            {
                memcpy(*pp, &val, sizeof(**pp));

                // load pp with the address of the 'next' member for
                //  the node we just allocated and saved. it will be
                //  the link to the next new node if we need one
                pp = &(*pp)->next;
            }
            else
            {   // malloc() failed. not good. no sense in continuing.
                perror("Failed to allocate new node.");
                break;
            }
        }
    }

    // required. this terminates the list.
    *pp = NULL; 
    return result;
}

解释

我们保留两个指针。一个是返回结果指针(result),最初为NULL。另一个是指针指针pp,它最初保存返回结果指针的地址。这是一个特殊变量。它是一个指向指针的指针,它总是保存我们在进行新分配时需要设置的 next 指针的地址。一张图片说千言万语。初始配置如下所示:

result = NULL;
^---- pp

当我们插入一个新节点时,我们会填充以pp为单位的地址指针。第一次该地址指向result。请注意取消引用*pp。因此,在添加新节点,然后将新节点的next指针的地址移动到pp后,图片现在看起来像这样:

result --> item0
           next
pp --------^

第二次插入后

result --> item0
           next --> item1
                    next
pp -----------------^

最后还有一个......

result --> item0
           next --> item1
                    next --> item2
                             next
pp --------------------------^

算法通过终止列表来完成。这是通过将pp寻址的指针设置为NULL来完成的。请注意那里的措辞。我们没有将pp设置为NULL(这将是毫无意义的)。我们将指针 it addresses 设置为NULL。

*pp = NULL;

结果图片将如下所示:

result --> item0
           next --> item1
                    next --> item2
                             next --> NULL

在每种情况下,插入口号总是相同的。

  • *pp
  • 指向的节点中存储新分配
  • 将数据复制到新分配
  • 存储 next 指针以接收pp
  • 中的下一个新分配
  • 循环直到我们读完文件。
  • 在退出循环时,将*pp设置为NULL,因为它是我们链中的 last next指针,并且应该为NULL以终止链接列表。< / LI>

有一些有趣的基本条件值得理解。值得注意的是“当输入文件有没有项目时会发生什么?这仍然有效吗?”是的,的确如此。在这种情况下,while循环将永远不会添加新项,pp仍将包含result的地址,*pp = NULL;将(冗余地)将其设置为NULL。结果将是一个NULL返回值,正是您想要的“空”链接列表。

强烈建议在调试器中运行此功能,以了解pp*pp**pp在此算法中的工作方式。

相关问题