使用devpartner的blockchecker检查c++内存错误

本文记录了一次在仿写STL过程中遇到的内存错误排查经历。通过使用devpartner工具定位到指针运算错误,发现是因为类型不匹配导致的数组越界问题。最终通过调整括号顺序解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在仿写stl的过程中,被一处内存错误卡了很久。当内存池需要多次malloc时会出现堆损坏的错误,初步判断是数组越界,但总是检查不出来。一开始用Dr.Memory检查不出来,就试了一下devpartner。官网链接 http://www.borland.com/zh-CN/Products/Software-Testing/Automated-Testing/Devpartner-Studio

  devpartner安装完后会自动在vs上装插件,但是那个插件好像没什么用。我是直接在安装目录下找到blockchecker,并用它打开要调试的exe,再点击运行,就能看到检测到的错误,如图

它提示这里指针运算范围错误,那为什么会错误呢?可以看到上面的代码

    *my_free_list = next = (obj *)chunk + n;

应为

    *my_free_list = next = (obj *)(chunk + n);

原来是忘了加括号了!!因为obj型指针和char型指针大小是不一样的,所以和n相加结果自然也不一样。一个括号浪费了我这么多时间。。。所以有个好的工具是多么重要。但是再好的工具也不能明确地指出是哪里出错,就像上面的例子,最后一步还是要我们自己推出来。

 1 //返回一个大小为n的对象,并可能加入大小为n的其他区块到freelist
 2 //在ANSI c中,void *不允许进行加减操作,所以chunk用char *
 3 void *default_alloc::refill(size_t n)
 4 {
 5     int objs = 20;
 6     char *chunk = chunk_alloc(n, objs);
 7     
 8     obj *next = nullptr, *current = nullptr;
 9     obj *result = nullptr;
10     obj **my_free_list = nullptr;
11     if (1 == objs)    //只取出一个区块
12         return chunk;
13     my_free_list = free_list + FREELIST_INDEX(n);
14     result = (obj *)chunk;    //这一块返回给客户端
15     //将freellist指向分配的区域
16     *my_free_list = next = (obj *)(chunk + n);
17     for (int i = 1;; i++)
18     {
19         current = next;
20         next = (obj *)((char *)next + n);    //这里注意不能直接用next+n
21         if (i == objs - 1)
22         {
23             current->next = nullptr;
24             break;
25         }
26         else
27             current->next = next;
28     }
29     return result;    
30 }
View Code

 

转载于:https://www.cnblogs.com/tonychen-tobeTopCoder/p/5193865.html

DevPartner Studio Professional Edition Release 11.4.0 , 含有静态代码分析、错误检测、性能、覆盖率和内存分析以及性能专家功能。 Visual Studio支持范围: Visual Studio 2005 SP1 ---- Visual Studio 2017 update 15.4。 windows支持范围: Server 2012 R2,基礎版; Windows 8.0,8.1; Server 2003 R2 SP2; Windows XP 專業版 SP3; Server 2008 SP2; Windows Vista SP2; Server 2008 R2,基礎版,SP1; Windows 7,基礎版,SP1; Windows 10(2017 年秋季)創作者更新。 DevPartner Studio Professional Edition is a suite of tightly integrated development features. DevPartner Studio incorporates code reviewing, error detection, two kinds of performance analysis, coverage analysis, and a memory analysis feature all integrated into Visual Studio. DevPartner Studio helps developers detect, diagnose, and resolve software bugs, maximize code performance, and ensure optimal code coverage and testing. 功能关键词:CodeReview;BoundsChecker;TrueCoverage;TrueTime; 新特性: 【New in Release 11.4】 Full Support for Windows 10.0 Fall Creators Update The product suite fully supports Windows 10.0 Fall Creators Update Full Support for Visual Studio 2017 update 15.4 DevPartner Studio 11.4 provides full Microsoft Visual Studio 2017 support. This includes full support the analysis of applications developed using Microsoft Visual Studio, and full integration into the Visual Studio 2017 IDE. Analyze, detect errors and perform static code reviews in existing supported project types and new Visual Studio 2017 project types. ================================== 用WinRAR 5.70 压缩的exe安装程序,建议用WinRAR 5.70及以上版本解压。 亲测在win7 x64 + vs2008sp1, win7 x64 + vs2015的环境下都可用。 此压缩包中只有安装本体程序 dpspe110400.exe。 ======= DevPartner License Manager 11.4.0 (x86 + x64): https://download.youkuaiyun.com/download/xiquanxyz/11104243 DevPartner Studio Professional Edition 11.4.0_官方文档: https://download.youkuaiyun.com/download/xiquanxyz/11104275
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值