【发布时间】:2021-08-24 07:59:02
【问题描述】:
我是编程新手,刚刚学习了 C。现在我的问题是,我试图 malloc 一个结构数组,然后用它来填写一些信息。但不断收到堆溢出错误报告。 这是我在 h.file 中声明的结构。
typedef struct llnode {
char llnodename[256];
int ll_index;
struct llnode* next;
} llnode;
//the struct of a linked list.
typedef struct node_stru {
char nodename[256];
int node_index;
} node_stru;
//the struct of the node.
和指针:
node_stru *node_list = (struct node_stru*)malloc(n_nodenumber*(sizeof(node_stru)));
但后来当我想使用链表填写一些信息时,它给了我堆溢出。
llnode* ptr=Ahead;
while (ptr!=NULL){
printf("the name%s, the index%d", ptr->llnodename, ptr->ll_index);
strcpy(node_list[n_index].nodename, ptr->llnodename);
node_list[n_index].node_index = ptr->ll_index;
n_index++;
ptr = ptr->next;
}
报错:我做了 malloc 一个 4*(256+4) 的内存,但还是不行。
0x619000000490 is located 0 bytes to the right of 1040-byte region [0x619000000080,0x619000000490)
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读the help pages,接受SO tour,阅读How to Ask,以及this question checklist。最后,请学习如何edit 您的问题以改进它们,例如向我们展示minimal reproducible example。还请在您的程序中包含可能的输入,或硬编码变量的值,例如
n_nodenumber。 -
没有什么能阻止循环从
node_list的末端走出来。