Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
malloc有时工作,有时不工作(通用链表)_C_List_Malloc - Fatal编程技术网

malloc有时工作,有时不工作(通用链表)

malloc有时工作,有时不工作(通用链表),c,list,malloc,C,List,Malloc,我正在创建通用链表。列表中的数据是从.txt文件(loadClients函数)加载的。问题是,当我将第二个元素推送到列表(push_back函数)时,malloc()有时不起作用,并且出现错误0xC0000005。例如,我运行该程序10次,效果良好,但第11次出现错误。我甚至无法检查malloc()返回的内容 以下是客户端结构: typedef struct client { int card_no; char last_name[30]; char first_name

我正在创建通用链表。列表中的数据是从.txt文件(loadClients函数)加载的。问题是,当我将第二个元素推送到列表(push_back函数)时,malloc()有时不起作用,并且出现错误0xC0000005。例如,我运行该程序10次,效果良好,但第11次出现错误。我甚至无法检查malloc()返回的内容

以下是客户端结构:

typedef struct client
{
    int card_no;
    char last_name[30];
    char first_name[30];
    int phone_no;
    char email[60];
    char login[18];
    char password[20];
    bool admin;
}client_struct;
以下是通用列表:

typedef struct List
{
    void *data;
    struct List *next;
}List;
以下是推回功能:

void push_back(List **head,void *data,size_t size)
{
    if(*head==NULL)
    {
        *head = (List *)malloc(sizeof(List));
        (*head)->data = malloc(size);
        (*head)->data = data;
        (*head)->next = NULL;
    }else
    {
        List *current = *head;
        while(current->next != NULL)
            current = current->next;
        printf("PUSH BACK1!\n");
        current->next = (List *)malloc(sizeof(List)); //HERE IS THE PROBLEM
        printf("PUSH BACK2!\n");
        current->next->data = malloc(size);
        current->next->data = data;
        current->next->next = NULL;
    }
}
这里是loadClients函数。此函数返回指向已创建列表的指针

List *loadClients()
{
    List *client_head = NULL;
    FILE *users = fopen("users.txt","r");
    if(users == NULL)
        return NULL;
    while(!feof(users))
    {
        client_struct *cl = malloc(sizeof(client_struct));
        memset(cl,0,sizeof(client_struct));
        char tab[200];
        memset(tab,0,200);
        fgets(tab,200,users);
        sscanf(tab,"%d %s %s %d %s %s %s %d",
    &cl->card_no,cl->last_name,cl->first_name,&cl->phone_no,
    cl->email,cl->login,cl->password,&cl->admin);
        printf("ADD\n");
        if(cl->card_no != 0)
            push_back(&client_head,(client_struct *)cl,sizeof(client_struct));
    }
    fclose(users);
    return client_head;
}

如何解决此问题?

使用调试信息(-g标志)编译:

然后通过valgrind运行它:

valgrind ./a.out

然后,valgrind将为您提供许多有用的信息,包括出错的原因、代码中的哪一行等等。

这不是您的主要问题,但请阅读。
(*head)->data=malloc(size)
(*头)->数据=数据看起来错误,是内存泄漏。我想你打算复制数据。为此,使用
(*head)->data=malloc(size)
memcpy((*head)->数据、数据、大小)。同样,对于使用
current->next->data
的另一个分支,关于我之前的评论,由于
push_back
被调用时使用了指向
malloc
d块的指针(从
loadClients()
中的
cl
变量),也许您不需要复制数据,只需删除
(*head)->data=malloc(size);
current->next->data=
malloc(size);`行,保留
(*head)->data=data;
current->next->data=data;
行。这样就不需要将
size
传递到
push_back
函数,除非您想将大小存储在
列表的其他成员中。感谢您指出,我改为memcpy(),但它仍然工作不好,但我还更改了loadClients()函数在while循环中。这次我使用了
client\u struct cl=EmptyStruct;
push\u back(&client\u head,(client\u struct*)&cl,sizeof(client\u struct));
并且运行良好(EmptyStruct是
静态构造client\u struct EmptyStruct;
)。非常感谢您的帮助!
current->next
这将在
之后始终为空,而(current->next!=NULL){…}
。结果将是
NULL=malloc
,但等待这是非法的。
valgrind ./a.out