常见运行时错误
1. terminate called after throwing an instance of ‘std::bad_alloc’ what (): std::bad_alloc 已放弃 (核心已转储)
下面这个错误,我的程序在数据量太大才出现的。分析了一下,确定以前遗留代码中频繁的使用 new 生成数组,再使用 delete [] 删除。最后修改为声明数组(我的程序只需要一个读写 buffer 区):
1
2
3terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
偶然遇到这个问题,通过虚拟机提升内存解决了
1.2 free(): invalid next size (fast)
不合理生成 malloc 的问题:
1*** glibc detected *** ./p_main: free(): invalid next size (fast): 0x08cec800 ***
我的情况是,声明一个 char 指针存放数据,未能考虑末尾的 ‘0’ , 故 free 释放出错:
1
2
3
4
5tmpdir = (char *)malloc (strlen (dir) + 1 + 1);
strcpy (tmpdir, dir);
if (tmpdir[strlen (tmpdir) - 1] != '/')
strcat (tmpdir, "/");
1我的总结:线程中如果频繁的使用malloc()即使正常的free(),这也是会出错的,要不就是段错误,要不就是
terminate called after throwing an instance of ‘std::bad_alloc’等等一些诡异的错误,我去,一定不能够频繁的使用 malloc () 哦,记住哦。频繁指的是:程序中频繁的调 malloc ()
2. 写文件的时候,千万不要写 string 到文件里面去阿,要不然呀,会高丝你的。