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

_CrtIsValidHeapPointer(pUserData)错误:Realloc()指针,c,pointers,realloc,singly-linked-list,C,Pointers,Realloc,Singly Linked List,在VS2012(C)中编译代码时,我收到了_CrtIsValidHeapPointer(pUserData)消息 我意识到我试图在指针上使用realloc,因此当我阅读其他帖子时,许多人建议使用memcpy和const char**。我真的很喜欢这样做,但我只是不知道怎么做 代码的第一个片段:我用于realloc int clientcounter = 0; //Global variable since this will be needed in other functions typed

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

我意识到我试图在指针上使用
realloc
,因此当我阅读其他帖子时,许多人建议使用
memcpy
const char**
。我真的很喜欢这样做,但我只是不知道怎么做

代码的第一个片段:我用于realloc

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;
我的程序读取文件的内容,并通过单链表将其存储到这些变量中

第二段代码:

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++
发生时,我都必须重新分配内存,这样它就不会泄漏。我的问题是,我知道错误的原因(重新分配指针),但我不知道如何修复它


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

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

需要什么

一般算法如下:

  • 从输入文件中读取一行
  • 将行解析为我们的结构成员
  • 如果正确解析了所有成员,请创建一个新项并将其添加到列表中
最后一步是上述代码的核心,我将在代码之后解释它是如何工作的。此修改后的函数将正在处理的文件名作为唯一的输入参数,并返回
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
),最初为空。另一个是指向指针的指针,
pp
,它最初保存返回结果指针的地址。这是一个特殊变量。它是一个指向指针的指针,它将始终保持我们在进行新分配时需要设置的下一个指针的地址。一幅画能说千言万语。初始配置如下所示:

result = NULL;
^---- pp
result --> item0
           next
pp --------^
result --> item0
           next --> item1
                    next --> item2
                             next --> NULL
当我们插入一个新节点时,我们将填充pp中的地址指针。第一次该地址将指向
结果
。请注意取消引用,
*pp
。因此,在添加新节点后,将新节点的
next
指针的地址移动到
pp
,现在的图片如下所示:

result = NULL;
^---- pp
result --> item0
           next
pp --------^
result --> item0
           next --> item1
                    next --> item2
                             next --> NULL
在第二次插入之后

result --> item0
           next --> item1
                    next
pp -----------------^
最后还有一个

result --> item0
           next --> item1
                    next --> item2
                             next
pp --------------------------^
算法结束时终止列表。这是通过将
pp
寻址的指针设置为空来实现的。注意这里的措辞。我们没有将
pp
设置为NULL(这将是毫无意义的)。我们正在将它所指向的指针设置为NULL

*pp=NULL

生成的图片将以如下方式结束:

result = NULL;
^---- pp
result --> item0
           next
pp --------^
result --> item0
           next --> item1
                    next --> item2
                             next --> NULL
在每种情况下,插入咒语总是一样的

  • *pp
  • 将数据复制到新分配
  • 将下一个指针存储在
    pp
  • 循环直到我们完成文件的读取
  • 退出循环后,将
    *pp
    设置为NULL,因为它是链中最后一个
    下一个
    指针,应该为NULL以终止链表
有一些有趣的基本条件值得理解。一个值得注意的问题是“当输入文件有项时会发生什么?这仍然有效吗?”是的,确实有效。在这种情况下,while循环永远不会添加新项,
pp
仍将包含
result
的地址,并且
*pp=NULL将(冗余)将其设置为空。结果将是一个空返回值,这正是“空”链接列表所需的值


我强烈建议在调试器中运行此程序,以查看
pp
*pp
**pp
如何在该算法中工作。

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

需要什么

一般算法如下:

  • 从输入文件中读取一行
  • 将行解析为我们的结构成员
  • 如果正确解析了所有成员,请创建一个新项并将其添加到列表中
最后一步是上述代码的核心,我将在代码之后解释它是如何工作的。此修改后的函数将正在处理的文件名作为唯一的输入参数,并返回
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
),最初为空。另一个是指向指针的指针,
pp
,它最初保存返回结果指针的地址。这是一个特殊变量。它是一个指向指针的指针,它将始终保持我们在进行新分配时需要设置的下一个指针的地址。一幅画能说千言万语。初始配置如下所示:

result = NULL;
^---- pp
result --> item0
           next
pp --------^
result --> item0
           next --> item1
                    next --> item2
                             next --> NULL
当我们插入一个新节点时,我们将填充pp中的地址指针。第一次该地址将指向
结果
。请注意取消引用,
*pp
。因此,在添加新节点后,将新节点的
next
指针的地址移动到
pp
,现在的图片如下所示:

result = NULL;
^---- pp
result --> item0
           next
pp --------^
result --> item0
           next --> item1
                    next --> item2
                             next --> NULL
在t之后