C 当我运行Valgrind时,我的代码中出现大小为8的无效写入错误

C 当我运行Valgrind时,我的代码中出现大小为8的无效写入错误,c,memory,valgrind,C,Memory,Valgrind,我也从Valgrind那里得到了这个信息 valgrind: m_mallocfree.c:280 (mk_plain_bszB): Assertion 'bszB != 0' failed. valgrind: This is probably caused by your program erroneously writing past the end of a heap block and corrupting heap metadata. If you fix any invalid

我也从Valgrind那里得到了这个信息

valgrind: m_mallocfree.c:280 (mk_plain_bszB): Assertion 'bszB != 0' failed.
valgrind: This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata.  If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away.  Please try that before reporting this as a bug.
这是我的代码,如果有人可以测试它

#include <stdio.h>
#include <stdlib.h>

//creating the node structure
typedef struct node
{
    char first_name[45];
    struct node *next_node;
}
node;

int main(void)
{
    // creating the pointer list and setting it to NULL;
    node *list = NULL;
    char name[20];
    // creating the first node
    node *n = malloc(sizeof(n));
    if(n == NULL)
    {
        printf("malloc cound't get enough mem.\n");
        return 1;
    }
    // dereferencing the name in node and the next_node pointer in n
    printf("Please print the users first name\n");
    // getting user input and storing it in first name
    scanf("%s",n->first_name);

    n->next_node = NULL;
    // having list point at n the first node
    list = n;
    // creating the tempory pointer we will use to make the linked list in a loop
    node *temp = NULL;

    // generating a singly linked list with a loop
    for(int i = 0; i < 7; i++)
    {
        temp = n;
        n = malloc(sizeof(node));
        if(n == NULL)
        {
            printf("malloc wasn't able to allocate the memory we needed. Aborting the program\n");
            return 1;
        }
        //dereferencing the new n node
        printf("Please print the users first name\n");
        // getting user input on the name and storing it in the first name array
        scanf("%s",n->first_name);
        n->next_node = temp;
        list = n;
    }

    // looping through the linked list and printing out the values
    for(node*tmp = list; tmp!=NULL;tmp=tmp->next_node)
    {
        printf("The name of the user is %s\n",tmp->first_name);

    }
    // freeing the linked list
    while(list!=NULL)
    {
        node *tempr = list->next_node;
        free(list);
        list = tempr;
    }

}
#包括
#包括
//创建节点结构
类型定义结构节点
{
char first_name[45];
结构节点*下一个_节点;
}
节点;
内部主(空)
{
//创建指针列表并将其设置为空;
节点*list=NULL;
字符名[20];
//创建第一个节点
node*n=malloc(sizeof(n));
如果(n==NULL)
{
printf(“malloc无法获得足够的内存。\n”);
返回1;
}
//取消引用节点中的名称和n中的下一个\u节点指针
printf(“请打印用户的名字\n”);
//获取用户输入并以名字存储
scanf(“%s”,n->first\u name);
n->next_node=NULL;
//在第一个节点上有n个列表点
列表=n;
//创建用于在循环中创建链表的临时指针
节点*temp=NULL;
//生成带有循环的单链表
对于(int i=0;i<7;i++)
{
温度=n;
n=malloc(sizeof(node));
如果(n==NULL)
{
printf(“malloc无法分配我们需要的内存。正在中止程序\n”);
返回1;
}
//取消对新n节点的引用
printf(“请打印用户的名字\n”);
//获取用户对名称的输入并将其存储在first name数组中
scanf(“%s”,n->first\u name);
n->next_node=temp;
列表=n;
}
//循环浏览链接列表并打印出值
对于(节点*tmp=list;tmp!=NULL;tmp=tmp->next_节点)
{
printf(“用户的名称是%s\n”,tmp->first\u name);
}
//释放链接列表
while(list!=NULL)
{
节点*tempr=list->next_节点;
免费(名单);
list=tempr;
}
}
我不知道如何纠正这个错误。如果有人能帮忙,我将不胜感激

node *n = malloc(sizeof(n));
您将保留指针大小作为参数的空间(当然不是您想要的),要保留结构本身大小的空间,请使用:

node *n = malloc(sizeof(*n));

正如您在
for
循环中所做的那样

您将保留指针大小作为参数的空间(当然不是您想要的),要保留结构本身大小的空间,请使用:

node *n = malloc(sizeof(*n));


正如您在
for
循环中所做的那样。

使用调试信息构建程序。这是GCC或Clang的
-g
标志。然后Valgrind会告诉您错误的确切位置。OT:about:
printf(“malloc无法获得足够的内存。\n”)错误消息应输出到
stderr
,而不是
stdout
。当错误来自C库时,函数还应输出系统认为错误发生的原因的文本。建议:
perror(“malloc无法获得足够的mem”)函数:
peror()
用于处理这两个活动使用调试信息构建程序。这是GCC或Clang的
-g
标志。然后Valgrind会告诉您错误的确切位置。OT:about:
printf(“malloc无法获得足够的内存。\n”)错误消息应输出到
stderr
,而不是
stdout
。当错误来自C库时,函数还应输出系统认为错误发生的原因的文本。建议:
perror(“malloc无法获得足够的mem”)函数:
peror()
用于处理这两个活动