大佬教程收集整理的这篇文章主要介绍了尝试 malloc 一个结构数组,但得到堆缓冲区溢出,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是编程新手,刚刚学习了 C。现在我的问题是,我试图 malloc 一个结构数组,然后用它来填充一些信息。但不断收到堆溢出错误报告。 这是我在 h.file 中的 struct 声明。
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 a 4*(256+4) 内存,但还是不行。
0x619000000490 is located 0 bytes to the right of 1040-byte region [0x619000000080,0x619000000490)
您已经为 node_list
分配了一个固定大小,但是没有什么可以阻止您的循环走到最后。例如,如果 n_nodenumber
为 4,您可以存储 4 个节点。但是如果 Ahead
链接到 5 个节点,它将离开 node_list
。假设 n_index
从 0 开始。
至少你的循环应该 assert(n_index < n_nodenumber)
以在循环离开数组时提供更好的错误。在没有看到完整示例的情况下,我只能说这些。
以上是大佬教程为你收集整理的尝试 malloc 一个结构数组,但得到堆缓冲区溢出全部内容,希望文章能够帮你解决尝试 malloc 一个结构数组,但得到堆缓冲区溢出所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。