程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 編程-C語言free問題,出現運行時錯誤CRT

編程-C語言free問題,出現運行時錯誤CRT

編輯:編程綜合問答
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)刪除即可。請問怎麼這是什麼問題,需要怎麼修改?

最佳回答:


樓主改了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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved