2 lin28170323 lin28170323 于 2015.06.17 16:43 提问

C语言free问题,出现运行时错误CRT

代码如下:

# include<stdio.h>
# include <stdlib.h>
# include <string.h>
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;
}

发生运行时错误,提示CRT detected that the application wrote to memory after end of heap buffer

只要把DeleteDeath函数的free(deletePeople)删除即可。请问怎么这是什么问题,需要怎么修改?

7个回答

lin28170323
lin28170323   2015.06.17 23:35
已采纳

楼主改了malloc的大小就可以了,当然代码还是要修改一下的。
贴上修改过的代码:

 # include<stdio.h>
# include <stdlib.h>
# include <string.h>
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(ListNode));

        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");

    linklist = footer;
    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-1;j++)
            slider = slider->next;

        deletePeople = slider->next;
        slider->next = deletePeople->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;
}
bdmh
bdmh   Ds   Rxr 2015.06.17 16:55

free要和new配对使用,你都没有new,怎么用free

lin28170323
lin28170323 你好,我是有用malloc的。
2 年多之前 回复
lin28170323
lin28170323 你好,我是有用malloc的。
2 年多之前 回复
ycg514230
ycg514230   Rxr 2015.06.17 18:22

他有malloc的,我刚刚下载了你的代码,在VS2012里面跑没问题啊。
除非你第二个参数比第一个大,那就会崩溃。

lin28170323
lin28170323 你好,我是用vs2015社区版和codeblocks都跑不了。
2 年多之前 回复
lin28170323
lin28170323   2015.06.17 20:06

csdn不知道怎么回复别人,就在这里回了。

回复bdmh:你好,我是有用malloc的。

回复帘卷西风:你好,我是用vs2015社区版和codeblocks都跑不了。

frank_20080215
frank_20080215   2015.06.17 21:43

deletePeople和malloc()不在同一个函数里,超出了作用范围

lin28170323
lin28170323 free不是根据指针去找的吗?
2 年多之前 回复
cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2015.06.18 00:25

图片说明
在我这里没有问题啊,

lin28170323
lin28170323 而且这代码其实还有点小问题,我上面贴了个修改过的。
2 年多之前 回复
lin28170323
lin28170323 ![图片说明](http://img.ask.csdn.net/upload/201506/18/1434589772_259147.png)
2 年多之前 回复
lin28170323
lin28170323 如果我不修改就是这样滴,因为在InitRing函数里给指针footer那里malloc错了。应该是sizeof(ListNode)
2 年多之前 回复
lin28170323
lin28170323   2015.06.18 09:11

图片说明

Csdn user default icon
上传中...
上传图片
插入图片