zoukankan      html  css  js  c++  java
  • C语言错误: HEAP CORRUPTION DETECTED

    程序源代码:

    //写文件两种方式(文本文件和二进制文件)
    
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //文本写文件
    int writeWord(const char *path,const char *pword){
        int ERRO_MSG = 0;
        if (path == NULL)
        {
            ERRO_MSG = 1;
            printf("path==NULL erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        if (pword == NULL)
        {
            ERRO_MSG = 2;
            printf("pword==NULL erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        //定义文件指针
        FILE *fpw=NULL;
        //打开文件
        fpw = fopen(path, "w");//w 打开只写文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件
        //判断文件是否打开成功
        if (fpw==NULL)
        {
            ERRO_MSG = 1;
            printf("文件打开失败 fpw==NULL erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        //开始写文件
        fputs(pword, fpw);
        //关闭文件
        if (fpw!=NULL)
        {
            fclose(fpw);
        }
        return ERRO_MSG;
    }
    
    //文件读文件
    char ** readtxt(const char *path)//二级指针做输出
    {
        int ERRO_MSG = 0;
        if (path==NULL)
        {
            ERRO_MSG = 1;
            printf("path==NULL erro msg:%d
    ", ERRO_MSG);
            //return ERRO_MSG;
        }
        //定义文件指针
        FILE *pfr = NULL;
        //打开文件
        pfr = fopen(path, "r");
        if (pfr==NULL)
        {
            ERRO_MSG = 3;
            printf("pfr==NULL erro msg:%d,文件路径:%s
    ", ERRO_MSG, path);
            //return ERRO_MSG;
        }
        //开始读文件
        //1.定义文件缓存数组
        char bufarr[100] = { 0 };
        //2.0 获取文本具体行数
        //2.1 定义临时变量
        int index = 0;
        //2.0定义返回二维数组
        char **resbuf = (char **)malloc(sizeof(char *)*5);
        //读文件
        while (!feof(pfr)){//feof()如果文件结束,则返回非0值,否则返回0
            memset(bufarr, 0, sizeof(char));
            fgets(bufarr, 100, pfr);
            char *bufstr1 = (char *)malloc(sizeof(char)*strlen(bufarr));
            //拷贝字符串
            strcpy(bufstr1, bufarr);
            resbuf[index] = bufstr1;
            index++;
            //resbuf = (char **)realloc(resbuf, sizeof(char *)*(index + 2));
        }
        resbuf[index] = NULL;
        return resbuf;
    }
    
    
    void main(){
    
    
    
        //定义文件路径
        char *path = "E:\Test\CwordTest\";//只适用于window
        char *path1 = "E:/Test/CwordTest/a1.txt";
        //writeWord(path1, "asfasdfasdgafdsgadf
    打倒日本帝国主义和所对符合大家的哟撒
    dsafgshfetgrhet");
        char **pdata = NULL;
        pdata=readtxt(path1);
        int index = 0;
        if (pdata!=NULL)
        {
            while (pdata[index] != NULL){
                printf("%s
    ", pdata[index]);
                //释放内存
                free(pdata[index]);
                pdata[index] = NULL;
                index++;
            }
            free(pdata);
            pdata = NULL;
            
        }
        system("pause");
    }

    报错页面

    错误解析:

     这个错误是在释放字符串指针的时候报错,具体行数如图

    错误原因:

    通过对错误进行百度,我理解了这个错误的原因是,释放一个字符串指针,但是这个字符串指针被破坏了,举例说明:

    char *str=(char *)malloc(sizeof(char)*10);//分配了10个字节大小的内存空间

    strcpy(str,"1234567890");//字符串拷贝,字符串”1234567890“是11个字符,这个拷贝操作会破坏字符指针str,此时会拷贝11字节到str指向的内存空间里,超过了原来分配的10个字节大小的内存空间

    如果这时候用free(str);释放内存就会报错  ---HEAP CORRUPTION DETECTED

    错误产生原因:

    这个错误产生的原因就是对strlen()这个函数理解的不到位,举例:

    char bufarr[100] = "1234567890";
    printf("%d ", strlen(bufarr));

    打印出10,但是如果你分配字符串应该分配11个,留一个字节给''

    而我上面就犯了这个错误

  • 相关阅读:
    logback 常用配置详解(二) <appender>
    logback 配置详解(一)
    logstash报错如下:Validation Failed: 1: this action would add [2] total shards, but this cluster currently has [999]/[1000] maximum shards open
    从字节码角度分析Byte类型变量b++和++b
    接口和抽象类有什么区别?你选择使用接口和抽象类的依据是什么?
    计算1至n中数字X出现的次数
    转:轻松搞定面试中的红黑树问题
    转:40个Java集合面试问题和答案
    自定义Adapter为什么会重复多轮调用getView?——原来是ListView.onMeasure在作祟
    何时调用getView?——从源码的角度给出解答
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5449032.html
Copyright © 2011-2022 走看看