程序源代码:

//写文件两种方式(文本文件和二进制文件)

#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 = ;
if (path == NULL)
{
ERRO_MSG = ;
printf("path==NULL erro msg:%d\n", ERRO_MSG);
return ERRO_MSG;
}
if (pword == NULL)
{
ERRO_MSG = ;
printf("pword==NULL erro msg:%d\n", ERRO_MSG);
return ERRO_MSG;
}
//定义文件指针
FILE *fpw=NULL;
//打开文件
fpw = fopen(path, "w");//w 打开只写文件,若文件存在,则文件长度清零,即文件内容会消失,若文件不存在则建立该文件
//判断文件是否打开成功
if (fpw==NULL)
{
ERRO_MSG = ;
printf("文件打开失败 fpw==NULL erro msg:%d\n", ERRO_MSG);
return ERRO_MSG;
}
//开始写文件
fputs(pword, fpw);
//关闭文件
if (fpw!=NULL)
{
fclose(fpw);
}
return ERRO_MSG;
} //文件读文件
char ** readtxt(const char *path)//二级指针做输出
{
int ERRO_MSG = ;
if (path==NULL)
{
ERRO_MSG = ;
printf("path==NULL erro msg:%d\n", ERRO_MSG);
//return ERRO_MSG;
}
//定义文件指针
FILE *pfr = NULL;
//打开文件
pfr = fopen(path, "r");
if (pfr==NULL)
{
ERRO_MSG = ;
printf("pfr==NULL erro msg:%d,文件路径:%s\n", ERRO_MSG, path);
//return ERRO_MSG;
}
//开始读文件
//1.定义文件缓存数组
char bufarr[] = { };
//2.0 获取文本具体行数
//2.1 定义临时变量
int index = ;
//2.0定义返回二维数组
char **resbuf = (char **)malloc(sizeof(char *)*);
//读文件
while (!feof(pfr)){//feof()如果文件结束,则返回非0值,否则返回0
memset(bufarr, , sizeof(char));
fgets(bufarr, , 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\r\n打倒日本帝国主义和所对符合大家的哟撒\r\ndsafgshfetgrhet");
char **pdata = NULL;
pdata=readtxt(path1);
int index = ;
if (pdata!=NULL)
{
while (pdata[index] != NULL){
printf("%s\n", 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\n", strlen(bufarr));

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

而我上面就犯了这个错误

C语言错误: HEAP CORRUPTION DETECTED的更多相关文章

  1. heap corruption detected错误解决方法调试方法以及内存管理相关

    1.heap corruption detected http://vopit.blog.51cto.com/2400931/645980   heap corruption detected:aft ...

  2. C语言错误: CRT detected that the application wrote to memory after end of heap buffer

    CRT detected that the application wrote to memory after end of heap buffer 多是中间对其进行了一些操作,在程序结束处,释放内存 ...

  3. HEAP CORRUPTION DETECTED:after Normal block错误方法解决

    一:问题描述: 出现的问题如下: 二:问题产生的原因说明 该问题发生于操作堆内存的时候.产生该问题的原因是:你实际使用的内存大小超出了你实际申请的内存大小,在释放内存的时候就会发生该问题. 举个例子: ...

  4. 内存溢出(heap corruption detected:)

    今天又遇到了上次出现的bug,然后百度了一下,想起来这是内存溢出的毛病,故记录下来! 出现的问题就是这样: heap corruption detected: after normal block(# ...

  5. VC++ 报错:Heap corruption detected

    今天在写代码时,发现莫名其妙的错误: std::string strName = L“testtest”; char* pOutString = new char(len + 1); Decrypt( ...

  6. HEAP CORRUPTION DETECTED :after Normal block 错误

    http://blog.csdn.net/zhccl/article/details/7889590

  7. heap corruption detected VS2015 C语言 报错

    申请动态内存时,申请的单元数为n,可用下标为0~n-1 但实际使用时超过了该范围,就会报这个错

  8. HEAP CORRUPTION DETECTED

    发生主要是由于这个问题给写入超出预分配的空间,注意检查越界情况 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  9. [linux系统]XFS (vda3):Corruption detected.Unmount and run xfs_repair 简单解决办法

    今天kvm的centos系统,rm -rf时报错,具体如下: rm: cannot remove ‘log-0/case1/log_net’: Input/output errorrm: cannot ...

随机推荐

  1. MyBatis入门(六)---mybatis与spring的整合

    一.整合需要 1.1.方法 上一章中的数据 需要spring通过单例方式管理SqlSessionFactory spring和mybatis整合生成代理对象,使用SqlSessionFactory创建 ...

  2. iOS开发笔记7:Text、UI交互细节、两个动画效果等

    Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...

  3. iOS UIButton添加圆角,添加边框

    //准备工作 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(,, ...

  4. IOS开发之SVN的使用

    关于SVN的一个使用细节    

  5. Swift中的字典

    学习来自<极客学院:Swift中的字典> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 import Foundation //字典的声明 v ...

  6. maven问题-"resolution will not be reattempted until the update interval of MyRepo has elapsed"

    最近在家里写maven程序的时候老是出现问题,有些问题到了公司就突然消失了. 在修改pom文件后保存的反应还是比较明显的,家里的网遇到有些依赖根本下载不了..墙. 但是到了公司,不但速度快,几乎啥都能 ...

  7. nodeJS创建工程

    转http://blog.csdn.net/i348018533/article/details/47258449 设置镜像地址 1.通过config命令 npm config set registr ...

  8. java中使用split分割字符串一个有趣的现象

    最近在项目中,发现了一个bug,充分了展示了自己对java底层的认知有很多的不足和欠缺. 下面有段代码: String str="1#2#3"; String[] strs=str ...

  9. setInterval setTimeout clearInterval

    setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout(). //第一次load的时候就先刷新一次 ...

  10. MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数

    安装MySQL时,有warning: [root@localhost mysql]# scripts/mysql_install_db --user=mysql Installing MySQL sy ...