Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

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


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
Posted
Updated 20-Nov-14 0:03am
v2

Just use std::vector:

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


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.
 
Share this answer
 
Comments
nv3 20-Nov-14 11:00am    
My 5.
I have solved it.

Use tr1::shared_ptr with custom deleter function.

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


C++
float *pDerVals = new float[Height*Width];
std::tr1::shared_ptr<float> apDerVals(pDerVals,array_deleter<float>());
 
Share this answer
 
v2
Comments
Aescleal 25-Nov-14 5:27am    
I wouldn't use shared_ptr for this sort of thing unless you know the performance implications of what you're up to. std::vector is almost always a better choice when you need a contiguous block of memory of a size determined at runtime.

Basically you have to balance the cost of copying the shared pointer against the cost of initialising the vector. The only way you're going to find that out is test them both in your use case and measure the performance.
hi,
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.
 
Share this answer
 
Comments
saqib.akhter 21-Nov-14 0:42am    
I tried this and it works but it changed the purpose of smart pointer.
then its better to delete pDerVals at the end of the function and no need to use auto_ptr.

float *pDerVals = new float[Height*Width];
std::auto_ptr<float> apDerVals(pDerVals);
float * p = apDerVals.release();

delete []p;
chandanadhikari 21-Nov-14 8:43am    
thats why i said that it looks lika a cheap trick :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900