作者:匍匐着前进中 | 来源:互联网 | 2023-09-16 03:32
发生运行时错误,提示CRTdetectedthattheapplicationwrotetomemoryafterendofheapbuffer只要把DeleteDeath
发生运行时错误,提示CRT detected that the application wrote to memory after end of heap buffer
只要把DeleteDeath函数的free(deletePeople)删除即可。请问怎么这是什么问题,需要怎么修改?
请大神们帮帮小白,指点一下,谢谢。
代码:
# include
# include
# include
typedef struct node {
char name[20];
struct node * next;
}ListNode;
typedef ListNode * LinkList;
LinkList InitRing(int totalNumberOfPeople, LinkList linklist)
{
ListNode *footer, *slider;//*footer是表尾,*slider是游标
char name[20];
int i;
footer = NULL;
linklist = slider = (ListNode *)malloc(sizeof(ListNode));
if (linklist == NULL)
{
printf("内存分配错误。");
exit(1);
}
getchar();
for (i = 1;i < totalNumberOfPeople;i++)
{
printf("第%d个人的名字\n", i);
scanf("%s",name);
strcpy(slider->name, name);
footer = (ListNode *)malloc(sizeof(LinkList));
if (footer == NULL)
{
printf("内存分配错误");
exit(1);
}
slider->next = footer;
slider = footer;
}
printf("第%d个人的名字\n", i);
scanf("%s",name);
strcpy(footer->name, name);
footer->next = linklist;
printf("\n");
return linklist;
}
LinkList DeleteDeath(int totalNumberOfPeople, int countOffLimits, LinkList linklist)
{
int i, j;
ListNode *slider, *deletePeople;
slider = linklist;
for (i = 1;i <= totalNumberOfPeople / 2;i++)
{
for (j = 1;j <= countOffLimits-2;j++)
slider = slider->next;
deletePeople = slider->next;
slider->next = deletePeople->next;
slider = slider->next;
printf("%s ", deletePeople->name);
if (i % 5 == 0) printf("\n");
free(deletePeople);
}
printf("\n");
return linklist;
}
void OutRing(LinkList linklist)
{
LinkList slider;
int i = 1;
slider = linklist;
do {
printf("%s ", slider->name);
if (i % 5 == 0) printf("\n");
i++;
slider = slider->next;
} while (slider != linklist);
}
int main()
{
int totalNumberOfPeople, countOffLimits;
LinkList linklist = NULL;
printf("请输入总人数:\n");
scanf("%d", &totalNumberOfPeople);
printf("请输入报数上限:\n");
scanf("%d", &countOffLimits);
linklist = InitRing(totalNumberOfPeople, linklist);//建立循环单链链表函数
printf("出局名单如下:\n");
linklist = DeleteDeath(totalNumberOfPeople, countOffLimits, linklist);
printf("\n");
printf("生存者名单如下:\n");
OutRing(linklist);
return 0;
}
5 个解决方案
你尝试单步跟一下,在崩溃前是不是指针指的空间已经是空的了,试图去free一个无效的地址