c++工作宝贵经验呀,我去。。。。

2. 常见运行时错误

2.1 terminate called after throwing an instance of 'std::bad_alloc'

下面这个错误,我的程序在数据量太大才出现的。分析了一下,确定以前遗留代码中频繁的使用 new 生成数组,再使用 delete[] 删除。最后修改为声明数组(我的程序只需要一个读写 buffer 区):

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted

2.2 free(): invalid next size (fast)

不合理生成 malloc 的问题:

*** glibc detected *** ./p_main: free(): invalid next size (fast): 0x08cec800 ***

我的情况是,声明一个 char 指针存放数据,未能考虑末尾的 '0' , 故 free 释放出错:

  tmpdir = (char *)malloc (strlen (dir) + 1 + 1);

strcpy (tmpdir, dir);
  if (tmpdir[strlen (tmpdir) - 1] != '/')
    strcat (tmpdir, "/");
 
 
 
 
我的总结:线程中如果频繁的使用malloc()即使正常的free(),这也是会出错的,要不就是段错误,要不就是

terminate called after throwing an instance of 'std::bad_alloc'等等一些诡异的错误,我去,一定不能够频繁的使用malloc()哦,记住哦。频繁指的是:程序中频繁的调malloc()

2.写文件的时候,千万不要写string 到文件里面去阿,要不然呀,会高丝你的。哦。我去。
法规的

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值