在VC9.0编译器中替换Auto_ptr用于内存分配数组 [英] Replacement of Auto_ptr for array of memory allocation in VC9.0 compiler

查看:50
本文介绍了在VC9.0编译器中替换Auto_ptr用于内存分配数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





需要建议解决以下场景

编译器VC9.0(visual Studio 2008)



Hi,

Need suggestion to resolve the below scenario
Compiler VC9.0 (visual Studio 2008)

float *pDerVals = new float[Height*Width];
auto_ptr<float> apDerVals(pDerVals);            // memory leak.





存在不匹配的内存分配/解除分配问题,因为auto_ptr将使用delete而不是delete []释放内容,因此auto_ptr不适合处理堆分配的数组(用new构造) ])并且仅适用于处理用new构造的单个堆分配数组。



因为我的编译器是VC9.0(visual Studio 2008)所以我无法使用Shared_ptr或Unique_ptr,这是非常古老的代码,并且很难重写整个功能。



有一种方法我知道如果我删除auto_ptr并使用delete [] oeprator释放内存,但这不是一个聪明的方法。



我不知道但是必须有某种方式通过使用矢量,但不知道如何使用这种情况。



如果您有任何解决方案,请提供一些解决方案。



谢谢,

Saqib



There is a mismatch memory allocation/deallocation issue because auto_ptr will free the content using delete instead of delete[], and so auto_ptr is not suitable for handling heap-allocated arrays (constructed with new[]) and is only suitable for handling single, heap-allocated arrays that were constructed with new.

Since my compiler is VC9.0(visual Studio 2008) so I couldn't use Shared_ptr or Unique_ptr and this is very old code and it's hard to rewrite the entire functionality.

there is a one way I came to know that if I remove auto_ptr and deallocate memory by using delete[] oeprator, but it won't be a smart way.

I don't know but there has to be some way by using vector but not sure how to use for this scenario.

Please suggest some solution if you have any.

Thanks,
Saqib

推荐答案

只需使用 std :: vector



Just use std::vector:

std::vector<float> values( height * width );





因为 vector 使用连续内存你可以只要你使用一个只有几处变化的指针就可以使用它。



PS:Visual C ++ 2008支持TR1所以你有shared_ptr和其他一些小玩意儿踢周围。你不想在这种情况下使用shared_ptr。



As vector uses contiguous memory you can use it wherever you'd use a pointer with only a few changes.

PS: Visual C++ 2008 supports TR1 so you've got shared_ptr and a few other doodads kicking around. You don't want to use shared_ptr for this kind of thing generally though.


我已经解决了它。



使用tr1 :: shared_ptr和自定义删除功能。



I have solved it.

Use tr1::shared_ptr with custom deleter function.

template< typename T >
struct array_deleter
{
  void operator ()( T const * p)
  {
    delete[] p;
  }
};







float *pDerVals = new float[Height*Width];
std::tr1::shared_ptr<float> apDerVals(pDerVals,array_deleter<float>());


hi,

这听起来像是一个廉价的解决方法:)。



在auto_ptr被破坏之前,你可以调用release(std :: auto_ptr :: release)。这将使auto_ptr apDerVals释放指针保持当前状态并指向null。

现在你可以销毁auto_ptr并调用* pDerVals上的delete []。



希望这有帮助。

This might sound like a cheap workaround :).

Before the auto_ptr gets destructed you can call release (std::auto_ptr::release) on it. This will make the auto_ptr apDerVals release pointer its holding currently and point to null.
Now you can destroy the auto_ptr and call delete[] on *pDerVals.

Hope this helps.


这篇关于在VC9.0编译器中替换Auto_ptr用于内存分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆