C语言free CRT detected that the application wrote to memory after end of heap buffer

C语言 码拜 9年前 (2015-07-10) 1093次浏览 0个评论


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

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

# 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;
}
 
补充一下,楼主用的是vs2015

10分

你尝试单步跟一下,在崩溃前是不是指针指的空间已经是空的了,试图去free一个无效的地址

30分

崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。

仅供参考:

//将c:\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
struct FB {
    char fn[256];
    size_t fl;
    char *b;
    struct FB *next;
    struct FB *prev;
} *fh,*fb,*ft;
char ln[256];
char fpn[256];
FILE *af;
FILE *f;
int L,n;
int main() {
    system("dir /b /a-d c:\tmp\*.* >c:\allfn.txt");
    af=fopen("c:\allfn.txt","r");
    if (NULL==af) {
        printf("Can not open file c:\allfn.txt!\n");
        return 1;
    }
    fh=NULL;
    fb=NULL;
    n=0;
    while (1) {
        if (NULL==fgets(ln,256,af)) break;
        L=strlen(ln);
        if (""\n""==ln[L-1]) ln[L-1]=0;
        printf("read %s\n",ln);
        strcpy(fpn,"c:\tmp\");
        strcat(fpn,ln);
        ft=(struct FB *)malloc(sizeof(struct FB));
        if (NULL==ft) {
            printf("Can not malloc ft!\n");
            fclose(af);
            return 2;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]==%p\n",n,ft);
        strcpy(ft->fn,fpn);
        f=fopen(fpn,"rb");
        if (NULL==f) {
            printf("Can not open file %s!\n",fpn);
            fclose(af);
            return 3;//之前的malloc在main退出后由操作系统自动free
        }
        ft->fl=_filelength(fileno(f));
        ft->b=malloc(ft->fl);
        if (NULL==ft->b) {
            printf("Can not malloc ft->b!\n");
            fclose(f);
            fclose(af);
            return 4;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]->b==%p\n",n,ft->b);
        if (ft->fl!=fread(ft->b,1,ft->fl,f)) {
            printf("fread error!\n");
            fclose(f);
            fclose(af);
            return 5;//之前的malloc在main退出后由操作系统自动free
        }
        fclose(f);
        ft->next=NULL;

        if (NULL==fh) {
            ft->prev=NULL;
            fh=ft;
        } else {
            fb->next=ft;
            ft->prev=fb;
        }
        fb=ft;
        n++;
    }
    fclose(af);
    printf("-----list-----\n");
    for (ft=fh;NULL!=ft;ft=ft->next) {
        printf("%8d %s\n",ft->fl,ft->fn);
        if (NULL!=ft) fb=ft;
    }
    printf("-----free-----\n");
    n--;
    if (NULL!=fh) {
        for (ft=fb->prev;NULL!=ft;ft=ft->prev) {
            if (NULL!=ft->next->b) {
                printf("ft[%d]->b==%p\n",n,ft->next->b);
                free(ft->next->b);
            }
            if (NULL!=ft->next) {
                printf("ft[%d]==%p\n",n,ft->next);
                free(ft->next);
            }
            n--;
        }
        if (NULL!=fh->b) {
            printf("ft[0]->b==%p\n",fh->b);
            free(fh->b);
        }
        printf("ft[0]==%p\n",fh);
        free(fh);
    }
    return 0;
}
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//找不到文件
//
//C:\tmp\tmp\Debug>tmp
//找不到文件
//-----list-----
//-----free-----
//
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//2011-06-30  18:04            44,840 my_c.rar
//2011-06-30  17:18             1,036 err.frm
//2011-06-30  14:32            14,243 出租.txt
//2011-06-28  12:08            23,681 MSDN98书签.txt
//             4 个文件         83,800 字节
//             0 个目录 17,041,870,848 可用字节
//
//C:\tmp\tmp\Debug>tmp
//read my_c.rar
//ft[0]==00421800
//ft[0]->b==00520068
//read err.frm
//ft[1]==00421670
//ft[1]->b==0052AFC0
//read 出租.txt
//ft[2]==00421530
//ft[2]->b==00378F28
//read MSDN98书签.txt
//ft[3]==004213F0
//ft[3]->b==0052B3F8
//-----list-----
// 44840 c:\tmp\my_c.rar
//  1036 c:\tmp\err.frm
// 14243 c:\tmp\出租.txt
// 23681 c:\tmp\MSDN98书签.txt
//-----free-----
//ft[3]->b==0052B3F8
//ft[3]==004213F0
//ft[2]->b==00378F28
//ft[2]==00421530
//ft[1]->b==0052AFC0
//ft[1]==00421670
//ft[0]->b==00520068
//ft[0]==00421800
//
//C:\tmp\tmp\Debug>
 
引用 2 楼 threshold1980 的回复:

你尝试单步跟一下,在崩溃前是不是指针指的空间已经是空的了,试图去free一个无效的地址

嗯,已经找到错了。是malloc时申请的大小有错。

 
引用 3 楼 zhao4zhong1 的回复:

崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。

仅供参考:

//将c:\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
struct FB {
    char fn[256];
    size_t fl;
    char *b;
    struct FB *next;
    struct FB *prev;
} *fh,*fb,*ft;
char ln[256];
char fpn[256];
FILE *af;
FILE *f;
int L,n;
int main() {
    system("dir /b /a-d c:\tmp\*.* >c:\allfn.txt");
    af=fopen("c:\allfn.txt","r");
    if (NULL==af) {
        printf("Can not open file c:\allfn.txt!\n");
        return 1;
    }
    fh=NULL;
    fb=NULL;
    n=0;
    while (1) {
        if (NULL==fgets(ln,256,af)) break;
        L=strlen(ln);
        if (""\n""==ln[L-1]) ln[L-1]=0;
        printf("read %s\n",ln);
        strcpy(fpn,"c:\tmp\");
        strcat(fpn,ln);
        ft=(struct FB *)malloc(sizeof(struct FB));
        if (NULL==ft) {
            printf("Can not malloc ft!\n");
            fclose(af);
            return 2;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]==%p\n",n,ft);
        strcpy(ft->fn,fpn);
        f=fopen(fpn,"rb");
        if (NULL==f) {
            printf("Can not open file %s!\n",fpn);
            fclose(af);
            return 3;//之前的malloc在main退出后由操作系统自动free
        }
        ft->fl=_filelength(fileno(f));
        ft->b=malloc(ft->fl);
        if (NULL==ft->b) {
            printf("Can not malloc ft->b!\n");
            fclose(f);
            fclose(af);
            return 4;//之前的malloc在main退出后由操作系统自动free
        }
        printf("ft[%d]->b==%p\n",n,ft->b);
        if (ft->fl!=fread(ft->b,1,ft->fl,f)) {
            printf("fread error!\n");
            fclose(f);
            fclose(af);
            return 5;//之前的malloc在main退出后由操作系统自动free
        }
        fclose(f);
        ft->next=NULL;

        if (NULL==fh) {
            ft->prev=NULL;
            fh=ft;
        } else {
            fb->next=ft;
            ft->prev=fb;
        }
        fb=ft;
        n++;
    }
    fclose(af);
    printf("-----list-----\n");
    for (ft=fh;NULL!=ft;ft=ft->next) {
        printf("%8d %s\n",ft->fl,ft->fn);
        if (NULL!=ft) fb=ft;
    }
    printf("-----free-----\n");
    n--;
    if (NULL!=fh) {
        for (ft=fb->prev;NULL!=ft;ft=ft->prev) {
            if (NULL!=ft->next->b) {
                printf("ft[%d]->b==%p\n",n,ft->next->b);
                free(ft->next->b);
            }
            if (NULL!=ft->next) {
                printf("ft[%d]==%p\n",n,ft->next);
                free(ft->next);
            }
            n--;
        }
        if (NULL!=fh->b) {
            printf("ft[0]->b==%p\n",fh->b);
            free(fh->b);
        }
        printf("ft[0]==%p\n",fh);
        free(fh);
    }
    return 0;
}
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//找不到文件
//
//C:\tmp\tmp\Debug>tmp
//找不到文件
//-----list-----
//-----free-----
//
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//2011-06-30  18:04            44,840 my_c.rar
//2011-06-30  17:18             1,036 err.frm
//2011-06-30  14:32            14,243 出租.txt
//2011-06-28  12:08            23,681 MSDN98书签.txt
//             4 个文件         83,800 字节
//             0 个目录 17,041,870,848 可用字节
//
//C:\tmp\tmp\Debug>tmp
//read my_c.rar
//ft[0]==00421800
//ft[0]->b==00520068
//read err.frm
//ft[1]==00421670
//ft[1]->b==0052AFC0
//read 出租.txt
//ft[2]==00421530
//ft[2]->b==00378F28
//read MSDN98书签.txt
//ft[3]==004213F0
//ft[3]->b==0052B3F8
//-----list-----
// 44840 c:\tmp\my_c.rar
//  1036 c:\tmp\err.frm
// 14243 c:\tmp\出租.txt
// 23681 c:\tmp\MSDN98书签.txt
//-----free-----
//ft[3]->b==0052B3F8
//ft[3]==004213F0
//ft[2]->b==00378F28
//ft[2]==00421530
//ft[1]->b==0052AFC0
//ft[1]==00421670
//ft[0]->b==00520068
//ft[0]==00421800
//
//C:\tmp\tmp\Debug>

这个够我看很久了,谢谢。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C语言free CRT detected that the application wrote to memory after end of heap buffer
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!